Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

MYSQL: Return any value on missing row.

Question posted by: pechar (Member) on July 1st, 2008 07:58 AM
Hi Guys,

I'm having problems returning a set of rows. What I have is two tables,
one to store properties and another storing the images (image paths) of the properties. Now a property isn't required to have an image. In that case I want to display an empty column or whatever (like null value). The thing is if a property doesn't have an image it's not listed in the images table. It's nonexistent.

I try to return all property adverts and each advert default image (only need the default one). I'm trying to achieve this in one SQL query below.

Code: ( text )
  1. SELECT  pa.property_id_pk,                       
  2.          pa.property_price,                       
  3.          pa.property_refno,                       
  4.          pa.property_bedrooms,                   
  5.          pa.property_description,                 
  6.          pa.property_featured,                   
  7.          pa.property_sale_rent,                     
  8.          pa.advert_active,                      
  9.          pa.advert_start_date,                   
  10.          pa.advert_end_date,                                           
  11.          pim.image_url                           
  12. FROM    tb_property_advert as pa                     
  13. INNER JOIN tb_property_image as pim               
  14. ON pa.property_id_pk=pim.property_advert_id_fk
  15. AND image_default = 1


Obviously this will only return the property adverts with a FK in the images table. What I need is to return the rest of property adverts (which have no image in the image table) and have it show as an empty column or null column.

Hope I explained myself well enough. Any ideas appreciated
Atli I think this is one for you :P

Thanks a lot
Luk
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
pechar's Avatar
pechar
Member
52 Posts
July 1st, 2008
08:07 AM
#2

Re: MYSQL: Return any value on missing row.
I Solved it!!! 2 mins after I posted here sorry!
All I had to do is a LEFT JOIN instead of an INNER JOIN as follows:

Code: ( text )
  1. SELECT  pa.property_id_pk,                       
  2.          pa.property_price,                       
  3.          pa.property_refno,                       
  4.          pa.property_bedrooms,                   
  5.          pa.property_description,                 
  6.          pa.property_featured,                   
  7.          pa.property_sale_rent,                     
  8.          pa.advert_active,                      
  9.          pa.advert_start_date,                   
  10.          pa.advert_end_date,                                           
  11.          pim.image_url                           
  12. FROM    tb_property_advert as pa                     
  13. LEFT JOIN tb_property_image as pim               
  14. ON pa.property_id_pk=pim.property_advert_id_fk
  15. AND image_default = 1


Please correct me if I'm wrong but it seems to be giving me the results I want!

Thanks anyway
Luk

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,944 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top MySQL Forum Contributors