• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

MySQL Syntax

Hi,

I have this:

Group, Genre
General, 80s
General, 90s
Electronic, Techno
Electronic, House

How can I display it like this:

General    80s
                 90s

Electronic
                 Techno
                 House
0
Computer Guy
Asked:
Computer Guy
  • 2
1 Solution
 
Daniel WilsonCommented:
Display issues (e.g. not repeating a category name) are handled in your front end code (e.g. PHP), not in the MySQL query.
0
 
Tomas Helgi JohannssonCommented:
Hi!

This is what you are looking for.
If the columns of the table is Group and Genre then

select `group`, group_concat(genre, ',' ) 
from table
group by `group`

Open in new window


Note that the word group is a reserved word in all dbms systems so using that in a table or view could lead to parsing errors when executing sql statements.
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Regards,
     Tomas Helgi
0
 
Computer GuyAuthor Commented:
Hi, group_concat(genre, ',' )  puts them next to eachother, how can I make them each on their own line?
0
 
Tomas Helgi JohannssonCommented:
This should do it
select `group`, group_concat(genre, '\n' ) 
from table
group by `group`

Open in new window


Regards,
     Tomas Helgi
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now