Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-27-2005, 05:27 AM   PM User | #1
quadrant6
Regular Coder

 
Join Date: Aug 2002
Posts: 124
Thanks: 0
Thanked 0 Times in 0 Posts
quadrant6 is an unknown quantity at this point
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?
quadrant6 is offline   Reply With Quote
Old 01-27-2005, 09:35 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
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
raf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:59 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.