Monday, March 12, 2012

MySQL Group_concat (transposing rows into columns)

To find out the columns in a table in mysql where you might need it programatically. You can use the information schema.

 SELECT group_concat(column_name) as columnNames

 FROM information_schema.columns

 WHERE 

     `TABLE_NAME` = 'EMPLOYEE_TABLE'

 

This would produce the  output as a single row instead of columns.

> EMP_ID,EMP_NAME,EMP_AGE

The group_concat transposes a result set from rows into columns, which is very ideal and can save some compute logic on the application side.

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat