Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Select multiple records in one line

Tags:

sql

mysql

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 |
-------------------------------
like image 919
Omar Avatar asked Dec 05 '25 19:12

Omar


1 Answers

Use GROUP_CONCAT

SELECT `Page`, GROUP_CONCAT(`Keyword` SEPARATOR ', ') AS 'foods'
FROM `Keywords` 
WHERE `Page` = 'food'
GROUP BY `Page`;
like image 75
Ende Neu Avatar answered Dec 07 '25 15:12

Ende Neu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!