If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Joining on non-null field?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-08, 06:48
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Joining on non-null field?

I'm close - probably - but not quite there. Basically:

Three tables: Projects, ProjectCategories, and ProjectImages.
  • Projects have a category_id.
  • Images have a project_id.

Images also have an is_default binary field - if it's 1, the image is the default image for its project. Only one image per project can be default. However, a project may not necessarily have any images associated with it.

So... I want to display a list of categories. But I want each category to have an image displayed with it. Seeing as categories and images aren't directly related - only through projects - I'm trying to join images to projects only where the image is default.

It's not working

This is what I've got:

Code:
SELECT c.id, c.category, i.thumb_small
FROM ProjectCategories c
INNER JOIN Projects p ON p.category_id = c.id
LEFT JOIN Images i ON i.project_id = p.id AND i.is_default = 1
GROUP BY c.category
ORDER BY c.category
It's returning NULL for some categories. I can only assume that this is because the first project it's in those categories doesn't have any images, but I don't know what do do about it.

Any suggestions?
Reply With Quote
  #2 (permalink)  
Old 12-08-08, 06:55
Spudhead Spudhead is offline
Registered User
 
Join Date: Jan 2002
Posts: 189
Oh, you fool. It's inner join, not left join.

Sorry
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On