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
https://forums.phpfreaks.com/topic/222590-help-with-table-join/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.