user3481058
user3481058

Reputation: 313

SQL join in one row in a query

I have two tables:

products(id1, productname)
commands(id2,#id1,username)

I want to show for example: for a particular username alex all the products(productname) that he bought in this format

  productname           |username
  dssd,dsds,sds         |Alex

I don't like to show all the products that he bought in multiple rows ! I want them just in one row. Please Can any one help. How we can do this with SQL?

Thanks in advance :)

Upvotes: 0

Views: 77

Answers (1)

Kickstart
Kickstart

Reputation: 21533

Using GROUP_CONCAT:-

SELECT a.username, GROUP_CONCAT(b.productname)
FROM commands a
INNER JOIN products b
ON a.id1 = b.id1
GROUP BY a.username

You can change the separator if required, eliminate duplicates and change the order of the concatenated items easily.

Upvotes: 2

Related Questions