我有这样的数据:
Category Product Mustards Hot Mustard Mustards Horseradish Mustard Mustards Honey Mustard Sauces Tomato Sauce Sauces Barbeque Sauce
我想得到这个输出:
Mustards Hot Mustard Horseradish Mustard Honey Mustard Sauces Tomato Sauce Barbeque Sauce
我以为我可以用GROUP BY和/或DISTINCT来做,像这样:
SELECT DISTINCT category, prodName FROM recipes GROUP BY category ORDER BY category, prodName ASC
但我运气不好。有人可以帮忙吗?
谢谢 - 乔
答案 0 :(得分:4)
试试这个:
SELECT category, GROUP_CONCAT(ProdName) as Product
FROM recipes
GROUP BY category
ORDER BY category, ProdName ASC
<强>输出:强>
CATEGORY PRODUCT ---------------------------------------------------------- Mustards Hot Mustard,Horseradish Mustard,Honey Mustard Sauces Tomato Sauce,Barbeque Sauce