Jump to content

LEFT JOIN confusion


ntroycondo

Recommended Posts

I have 3 tables for image DB.

I want to return photo name and photographer name for photos taken in Chicago.

3 tables are:

HPhotograher with columns photographerid, name

HLocation with columns locationid, name

HPhoto with columns photoid, filename, photographerid, locationid

 

My below query returns empty set with errors. So i know I'm confusing the query somehow.

 

 

SELECT HPhoto.filename, HPhotographer.name

FROM HPhotographer

LEFT JOIN HPhoto ON HPhoto.photographerid = HPhotographer.photographerid

LEFT JOIN HLocation ON HLocation.locationid = HPhoto.locationid

WHERE  HLocation.locationid = HPhoto.locationid AND HPhoto.locationid = 'chicago'

ORDER BY HPhoto.date ASC;

 

Link to comment
Share on other sites

I don't see why you need a LEFT JOIN. A regular JOIN should do it.

1) Take out the first condition in the WHERE clause, you are just repeating the relationship already stated in the second JOIN ... ON;

2) is locationid a string or a number? Don't you want the name from the location table in the WHERE clause?

 

 

SELECT HPhoto.filename, HPhotographer.name
FROM HPhotographer
JOIN HPhoto ON HPhoto.photographerid = HPhotographer.photographerid
JOIN HLocation ON HLocation.locationid = HPhoto.locationid
WHERE  HLocation.name = 'chicago'
ORDER BY HPhoto.date ASC;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.