问题
I have data like this:
Category Product Mustards Hot Mustard Mustards Horseradish Mustard Mustards Honey Mustard Sauces Tomato Sauce Sauces Barbeque Sauce
And I'd like to get this output:
Mustards Hot Mustard Horseradish Mustard Honey Mustard Sauces Tomato Sauce Barbeque Sauce
I thought I could do it with GROUP BY and/or DISTINCT, like this:
SELECT DISTINCT category, prodName FROM recipes GROUP BY category ORDER BY category, prodName ASC
but I'm not having much luck. Can anyone help?
Thanks - Joe
回答1:
Try this:
SELECT category, GROUP_CONCAT(ProdName) as Product
FROM recipes
GROUP BY category
ORDER BY category, ProdName ASC
Output:
CATEGORY PRODUCT ---------------------------------------------------------- Mustards Hot Mustard,Horseradish Mustard,Honey Mustard Sauces Tomato Sauce,Barbeque Sauce
See this SQLFiddle
来源:https://stackoverflow.com/questions/11928360/output-mysql-list-of-records-grouped-by-category