Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subquery to String?

Tags:

mysql

subquery

I have a table containing countries:

id     country
------------
0     Monaco 
1     Mongolia
2     Montenegro
3     Morocco
4     Mozambique
5     Myanmar

I have a sub query that looks like this.

(SELECT country FROM COUNTRIES WHERE id < 10) AS ´Trip´

I want to have that subquery to be formatted as a string like this:

'Monaco, Mongolia, Montenegro, Morocco, Mozambique, Myanmar'

Is that possible?

like image 334
jantimon Avatar asked May 14 '26 07:05

jantimon


1 Answers

You can use the group_concat function:

SELECT group_concat(country, ', ') FROM countries WHERE id < 10
like image 142
Lukáš Lalinský Avatar answered May 16 '26 08:05

Lukáš Lalinský



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!