简体   繁体   中英

MySQL: Select multiple records in one line

In a query like this one:

SELECT * 
FROM `Keywords`
WHERE `Page` = 'food'

My results are displayed like so:

| Page   | Keyword |
--------------------
| food   | Pizza   |
--------------------
| food   | Burger  |
--------------------
| food   | Sushi   |
--------------------

How do I write my SQL statement, to get a result like this one?:

| Page | Keyword              |
-------------------------------
| food | Pizza, Burger, Sushi |
-------------------------------

Use GROUP_CONCAT

SELECT `Page`, GROUP_CONCAT(`Keyword` SEPARATOR ', ') AS 'foods'
FROM `Keywords` 
WHERE `Page` = 'food'
GROUP BY `Page`;

Try this:

SELECT page, group_concat(Keyword separator ', ') as myList
FROM 'Keywords'
WHERE `Page` = 'food'
GROUP BY Keyword;

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM