Forum Moderators: coopster
I was wondering if there is a way to organize fields with the same value like so:
database-
row 1:
field 1(name): Yamaha. (same name as below)
field 2(item): Speakersrow 2:
field 1(name): Yamaha. (same name as above)
field 2(item): KeyboardsOutput:
Yamaha: Speakers, Keyboards
thanks,
electricocean
SELECT `name`,`item` FROM `table` GROUP BY `name`
That should return them grouped by 'Yamaha', so you will have all the e.g. Yamaha first, then the next brand, etc.. however it will still say the brand with each of multiple rows.
If you really don't want to do it like that, then you nest two queries in a loop:
$dh=mysql_query("SELECT DISTINCT `name` FROM `table`");
while ($r=mysql_fetch_assoc($dh))
{
print "Name: $r[name]<br>";
$dh2=mysql_query("SELECT DISTINCT `item` FROM `table` WHERE `name`=$r[name]");
while ($r2=mysql_fetch_assoc($dh))
{
print " - Item: $r2[item]<br>";
}
}
SELECT
field1,
GROUP_CONCAT(field2 ORDER BY field2 SEPARATOR ', ') as field2
FROM mytable
GROUP BY field1
;