kuki
kuki

Reputation: 79

SQL/PHP get a Selection from 2 Tables

I have 2 Tables and i need to hide a multiple result.

My Tables:

aID Name
 1  aaa
 2  bbb


aID Stuff
 1   01
 1   02
 1   06
 2   01
 2   03

My result looks like this:

 1   aaa  01
 1   aaa  02
 1   aaa  06
 2   bbb  01
 2   bbb  03

How can i display the result in HTML/PHP that it looks like this:

1 aaa 01,02,06
2 bbb 01,03

Upvotes: 0

Views: 42

Answers (3)

Mudassar Saiyed
Mudassar Saiyed

Reputation: 1148

This is just the syntax change it as per your need. Hope this may help you.

select id, group_concat(`Name` separator ',') as `ColumnName`
from
(
select id, concat(`Name`, ':',
group_concat(`Value` separator ',')) as `Name`
from mytbl
group by id, `Name`
) tbl
group by id;

Upvotes: 0

DieVeenman
DieVeenman

Reputation: 457

Add this to your query

GROUP_CONCAT(Stuff) As Stuff //Where you select your Stuff

GROUP BY aID //At the end

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

Try using group_concat along with group by clause, instead of using HTML/PHP you can simply get it from sql using query as

select a.aID, a.Name, Group_concat(b.stuff) as stuff
from user a join stuff b on a.aID = b.aID
group by aID
order by aID, Name ASC

Note: Table name are arbitrary over here please place your table names

Upvotes: 1

Related Questions