![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
Regular Coder ![]() Join Date: Aug 2002
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
SQL/PHP question (Grouping results)
First off, I have 2 simple tables:
region region_id region_name item item_id region_id item_name I wish to list on my page all regions with items belonging to each region displayed underneath. To complicate, If a region has no items then it should still be listed. I have pissed around and can't get it to work if a region contains no items. Can anyone offer some insight on the PHP/SQL needed to achieve this? |
|
|
|
|
|
PM User | #2 |
|
Master Coder ![]() ![]() Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
you need to use an outer-join so that all regions are included in the recordset. Like
SELECT region.region_name, item.item_name FROM region LEFT JOIN item ON region.region_id=item.region_id ORDER BY region.region_id the order by clause is to allow you to display the items like you want. You just compaire the region_name to the one of the previous record (stored in a variable) and only display it if it's different from the previous record. if you have a mysql version of 4.1 or up, then you can use a group_concat().
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|