I stumbled across a neat new (to me, anyway) GROUP BY aggregate function in mysql: it's called GROUP_CONCAT. (It's described here:
MySQL 5.0 Reference Manual :: 12.10.1 GROUP BY (Aggregate) Functions)
Here's how I used it: For the
FLOSSMole project, I was constructing a list of projects on Sourceforge, the programming language(s) used on the project, and the number of developers on the project.
Normally, we would design a SQL query like this:
SELECT p.proj_unixname, ppl.description, p.dev_count
FROM projects p, project_programming_language ppl
WHERE p.proj_unixname = ppl.proj_unixname
AND p.datasource_id =19
GROUP BY 1, 2
ORDER BY 1Which yields results like:
| project #1 | perl | 1 |
| project #1 | java | 1 |
| project #2 | python | 14 |
| project #3 | python | 2 |
| project #3 | C++ | 2 |
In this case, we have project #1 which has 2 programming languages (perl, java), and 1 developer. We have project #2 which has one language (python) and 14 developers. We have project #3 which has 2 languages (python C++) and 2 developers.
The number of rows in the table for that project is determined by the number of unique languages. So, a project with 4 languages will have 4 rows. I was hoping there was a way to group these languages together, and indeed I found one: GROUP_CONCAT.
With this new function, we can fix this so that each project will only show one row in the output table. The languages are concatenated together before printing. The query can look like this:
SELECT p.proj_unixname, p.dev_count,
GROUP_CONCAT(DISTINCT ppl.description)
FROM projects p, project_programming_language ppl
WHERE p.proj_unixname = ppl.proj_unixname
AND p.datasource_id =19
GROUP BY 1, 2
ORDER BY 1 And the output will look like:
| project #1 | 1 | perl, java |
| project #2 | 14 | python |
| project #3 | 2 | python,C++ |