Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (20.3k points)

I need to run a query like:

SELECT p.id, p.name, 

       (SELECT name 

          FROM sites s 

         WHERE s.id = p.site_id) AS site_list

  FROM publications p

But I'd like the sub-select to return a comma-separated list, instead of a column of data. Is this even possible, and if so, how?

1 Answer

0 votes
by (40.4k points)

Try using GROUP_CONCAT to return the comma-separated list, instead of the column of data perform like this:

SELECT p.id, p.name, GROUP_CONCAT(s.name) AS site_list

FROM sites s

INNER JOIN publications p ON(s.id = p.site_id)

GROUP BY p.id, p.name;

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 22, 2020 in SQL by Sudhir_1997 (55.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.6k users

Browse Categories

...