join

How to use SQL SELECT, GROUP BY, ORDER BY, and COUNT (all in one)

Don’t tell anyone, but my SQL skills are pretty average these days, at best, mostly because I haven’t had to do anything hard in a while. But just now I was happy to write this little SQL SELECT query that does a GROUP BY, an ORDER BY, and a COUNT, yielding the results shown in the image:

select nid, count(nid) from term_node
where tid in (3,1,11,10,9,8,7)
group by nid
order by count(nid) DESC

I’m going to use this query — or one very similar to it — to get a list of nodes (nid) that have the most tag ids (tid) from the list of tid in the query. In theory, the nodes (blog posts) that have the most tags in common should be the most related to each other. So, in my Scrupal6 replacement for Drupal 6, this query is a way to get “related” content for a given blog post. (The tid list shown comes from node id 4, so I need to also exclude nid=4 from the results. I also need to add a limit clause to the query.)

If you ever need to do a group by, order by, and count in one SQL query, I hope this example is helpful.

How to merge Scala sequential collections (List, Vector, ArrayBuffer, Array, Seq)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.22, “How to Merge Scala Sequential Collections”

Problem

You want to join two sequences into one sequence, either keeping all of the original elements, finding the elements that are common to both collections, or finding the difference between the two sequences.

Solution

There are a variety of solutions to this problem, depending on your needs:

Scala - Merging two Arrays or ArrayBuffers alvin October 19, 2012 - 12:09am

Scala Array FAQ: How do I merge two Arrays or ArrayBuffers?

Solution: Use the ++ method to join two arrays into one new array:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> val c = a ++ b
c: Array[Int] = Array(1, 2, 3, 4, 5, 6)

ArrayBuffer

Use the same approach to merge two ArrayBuffers into a new ArrayBuffer:

Perl array printing: How do I print the entire contents of an array with Perl?

Perl array FAQ: How do I print the entire contents of an array in Perl?

To answer this question, we first need a sample Perl array. Let's assume that you have an array that contains the name of baseball teams, like this:

@teams = ('cubs', 'reds', 'yankees', 'dodgers');

Perl array printing

Now, if you just want to print the array with the array members separated by blank spaces, you can just print the array like this:

Where is Perl looking for modules to include? alvin August 16, 2008 - 7:59am

I started working on a new Unix system yesterday -- an HP-UX system I've never worked on before -- and I quickly realized that I needed some Perl modules installed. While working with another person the question quickly came up, "How do you know where Perl is looking for currently installed modules?"

AppleScript string tip: How to concatenate strings

AppleScript string FAQ: How do I concatenate (merge) strings in AppleScript?

Fortunately string concatenation in AppleScript is pretty easy (if not a little different). To concatenate strings in AppleScript just use the ampersand (&) operator.

Here are a few AppleScript string concatenation examples, with a dialog thrown in so you can see the result:

How to print a Perl array alvin June 22, 2002 - 6:46am

Summary: How to use a Perl foreach loop to print every element in a Perl array.

To look at how to print every element in a Perl array using the foreach operator, the first thing we need is a sample array. Let's assume that you have an array that contains the name of baseball teams, like this:

@teams = ('cubs', 'reds', 'yankees', 'dodgers');

If you just want to print the array with the array members separated by blank spaces, you can just print the array like this: