Jump to content

Help with Table Join


mds1256

Recommended Posts

$nid = 65        ------- an example

 

 

Select title, body, picdescription, piclink, date_format(date, '%D %M %Y' ) as convDate from news, newspic where news.id = $nid and newspic.newsid = $nid

 

This query works perfect when the linked record is in both tables, but if the record does not exist in the 'newspic' table it doesnt even return the result from the 'news' table.

 

Please bear in mind the way this works is to return all the results from 'newspic' table (which could be multiple results) where the news.id and newspic.newsid matches.

 

Any idea how i can return the result even if the newspic table contains no matching records

 

I have tried an LEFT JOIN but cannot work out the exact way to do it?

 

Thanks

 

 

Link to comment
Share on other sites

try:

SELECT title, 
       body, 
       picdescription, 
       piclink, 
       date_format(date, '%D %M %Y' ) as convDate 
FROM news
    LEFT JOIN newspic ON newspic.newsid = news.id
WHERE news.id = $nid

 

assuming that newspic.newsid is the foreign key to news (newspic.newsid = news.id).... otherwise replace with the right field holding the relationship.

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.