我想选择类别以及产品我对同一产品有多个类别,并且我试图以列表方式显示,
我确实尝试过使用子查询和加入,但是我获得了同一产品的重复记录
我希望得到的结果是逗号分隔一行中同一产品的所有类别
我试图获取看起来如下的数据
--------------------------------------------------------------
PRODUCT_NAME CATEGORY
--------------------------------------------------------------
Female Shoes - Blue color Footwear, Sunglass
Female Sunglass Yellow color Sunglass, Fashion, Eye protection
这是我的SQL Fiddle链接
答案 0 :(得分:3)
:
SELECT
P.PRODUCT_NAME,
GROUP_CONCAT(C.CATEGORY_NAME separator ',')
FROM
PRODUCT_CATEGORY PC
LEFT JOIN PRODUCT P ON (P.PRODUCT_ID = PC.PRODUCT_ID)
LEFT JOIN CATEGORY C ON (C.CATEGORY_ID = PC.CATEGORY_ID)
GROUP BY
P.PRODUCT_NAME
ORDER BY
P.PRODUCT_NAME ASC
Oracle 11g中的:
SELECT
P.PRODUCT_NAME,
wm_concat(C.CATEGORY_NAME)
FROM
PRODUCT_CATEGORY PC
LEFT JOIN PRODUCT P ON (P.PRODUCT_ID = PC.PRODUCT_ID)
LEFT JOIN CATEGORY C ON (C.CATEGORY_ID = PC.CATEGORY_ID)
GROUP BY
P.PRODUCT_NAME
ORDER BY
P.PRODUCT_NAME ASC