Wednesday, February 25, 2009

Mysql and group_concat!

I'm determined to write a little more often on my blog (I've skipped 2008 altogether) because it also works as my personal notepad for the world to see. So let's get started.

Today I've discovered a wonderful new MySQL trick: group_concat and it saved me from doing tedious/slow scripting and also from trying to understand how Stored Procedures work in MySQL (which is something like arameic to me right now).

I've been working with Eventum which is a lovely open source bug tracker from the great thinkers at MySQL AB (now.. Sun). Eventum lets you add N custom fields to the issue creation and listing page, and it adds all of them to a table called eventum_issue_custom_fields. very much like this:


Each custom field has an Id, but they are all entered in different "rows" like this:
(lets say custon field "1" is the tv show name and field "2" is a short description)







IssueIdCustomFieldIdCustomFieldData
50190210
502Lame TV show
511Family Guy
512Funny Stuff


what if i wanted to select from this table the tv show name and the description in just one row?

Thinking about loops in your scripts?
Thinking about stored procedures?
Thinking it cannot be done at all?

Check this out:

SELECT IssueId,GROUP_CONCAT(CustomFieldData ORDER BY CustomFieldData SEPARATOR ',') AS value_list FROM tv_show_table GROUP BY IssueId ORDER BY IssueId

And you will get some nice stuff like:




IssueIdvalue_list
5090210, Lame TV Show
51Family Guy, Funny Stuff



How cool is that?!

the actual eventum query would be:

SELECT icf_iss_id,group_concat(icf_value order by icf_value separator ',') as values_list from eventum_issue_custom_field where icf_fld_id=2 OR icf_fld_id=1 group by icf_iss_id order by icf_iss_id;

Because i only want fields with id 1 or 2.


Bye!

Friday, February 20, 2009

Freetds, DBD-Sybase, Fedora 10


This is an update to an earlier entry

If you want to install DBD-Sybase Perl Module and use freetds in Fedora 10
you must install a patch for it to work. My versions are freetds-0.82, DBD-Sybase-1.09 and of course Fedora 10.




Find your way to the FreeTDS source directory, such that this command
works:

$ grep '\$Id' include/cspublic.h
static const char rcsid_cspublic_h[] = "$Id: cspublic.h,v 1.61 2008/09/08
17:50:25 jklowden Exp $";

Your ID will be different; it should match what's in the patch: 1.58.

Then "man patch" is your friend. Like it says,

usually just

patch
is all you need, as in:

$ patch < /path/to/your/cspublic.BLK_VERSION_150.patch

Then build and install per usual.


(shamelessly stolen from http://lists.ibiblio.org/pipermail/freetds/2009q1/024260.html)


Enjoy!!