我有2个表格如下所示
product_category表
╔═════════════╦═════════════════╗
║ category_id ║ parent_id ║
╠═════════════╬═════════════════╣
║ 1 ║ 8,7 ║
║ 2 ║ 8 ║
╚═════════════╩═════════════════╝
product_category_description表
╔═════════╦═══════════╦═════════╗
║ id ║category_id║ name ║
╠═════════╬═══════════╬═════════╣
║ 1 ║ 1 ║ Test ║
║ 2 ║ 2 ║ Test1 ║
║ 3 ║ 3 ║ Test2 ║
║ 4 ║ 7 ║ Test3 ║
║ 5 ║ 8 ║ Test4 ║
╚═════════╩═══════════╩═════════╝
我想要输出如下所示
╠══════════╬═══════════════╣
║ 1 ║ Test4, Test3 ║
║ 2 ║ Test4 ║
╚══════════╩═══════════════╝
我尝试过以下查询但没有工作:
SELECT GROUP_CONCAT(b.category_name) cat_name
FROM product_category a
INNER JOIN product_category_description b
ON FIND_IN_SET(a.parent_id, b.category_id) > 0
GROUP BY b.category_id
答案 0 :(得分:2)
除group by
字段
SELECT GROUP_CONCAT(b.category_name) cat_name
FROM
product_category a
INNER JOIN
product_category_description b
ON FIND_IN_SET(a.parent_id, b.category_id) > 0
GROUP BY
a.category_id
答案 1 :(得分:2)
使用以下查询我用我的数据库检查
SELECT a.category_id,GROUP_CONCAT(DISTINCT b.name)
FROM product_category_description AS b
INNER JOIN `product_category` as a
ON FIND_IN_SET(b.category_id,a.parent_id)
GROUP BY category_id