Hy, i have a big question … it may sound simple but actually it’s very complicated
I have two mysql tables :
info_cat (id, name)
info_subcat (id, id_info_cat, name)
I want to show the user the info_cat.name followed by info_subcat.name but the problem is that info_cat.name i want to appear just one time.
Something like this:
Laptop
- Hp
- Sony
- Acer
HDD
- Western DIgital
- Seagate
- ETC
So normaly i will query like this (old fashioned)
$query = mysql_query("SELECT * FROM info_cat");
while ($row = mysql_fetch_assoc($query)) {
$id_info_cat = $row['id'];
echo $row['name'];
$query2 = mysql_query("SELECT * FROM info_subcat WHERE id_info_cat = '$id_info_cat'");
while($row2 = mysql_fetch_assoc($query2)) {
echo $row2['nume'];
}
}
and i have while within while.
My question is how can i use only a query to acomplish this (i think with JOIN) ??
I have tried the following command :
SELECT info_cat.name, info_subcat.name AS name2, info_subcat.id FROM info_cat, info_subcat WHERE info_cat.id = info_subcat.id_info_cat;
but can’t figure it out how to manipulate data because it will output :
name name2 id
Laptop Acer 1
Laptop HP 2
Laptop Sony 3
HDD Western Digital 4
HDD Seagate 5
HDD ETC 6
and i just want to echo Laptop one time and HDD one time followed by its subcat names.
How can i do that ?
Pleaseee ….
Thank you
Try using GROUP BY and GROUP_CONCAT.