0

I have two tables - category and subcategory, they both have id_category.
In other words, in category:

id_category: 3
category_name: Security

And in subcategory:

id_subcategory: 1
id_category: 3
subcategory_name: Antivirus

I have multiple items in the same category, so I want it to list the items like this:
Security: Antivirus, Spyware, Firewall.

Also, I want it to do the same with other categories, so in the end it will list like this:
Security: Antivirus, Spyware, Firewall.
Multimedia: Audio, Video, Images.

I'm not sure how to do that. I searched around and tried different things but nothing worked for me.

4

1 に答える 1

2

GROUP_CONCAT集計関数を使用できます。

SELECT
  category_name,
  GROUP_CONCAT(subcategory_name)
FROM
  category INNER JOIN subcategory
  ON subcategory.id_category=category.id_category
GROUP BY
  category_name

ここでフィドルを参照してください。

于 2013-02-15T19:11:34.533 に答える