RobinV91nl
RobinV91nl

Reputation: 53

Join table with multiple rows to one row

I have a database that looks like:

TABLE 1
ID | NAME | PRICE

TABLE 2
TABLE1.ID | ITEM

As you can see it is possible that table 2 can contain multiple references to table 1.

Is it possible to create a query that gives a result like this?

TABLE1.ID | NAME | PRICE | TABLE2.ITEM REC 1 |  TABLE2.ITEM REC 2 |  TABLE2.ITEM REC 3

Upvotes: 0

Views: 429

Answers (2)

Himanshu
Himanshu

Reputation: 32602

Try this one:

SELECT t1.*, GROUP_CONCAT(t2.ITEM) AS Items
FROM Table1 t1 
JOIN Table2 t2
  ON t1.ID = t2.TABLE1_ID
GROUP BY t1.ID

See this SQLFiddle

Upvotes: 1

Alin Breaban
Alin Breaban

Reputation: 284

Consider looking at this MySQL function: GROUP_CONCAT(expr). It will sure answer your question

Mysql Documentation - group_concat()

Upvotes: 2

Related Questions