简体   繁体   中英

MySQL: Using JOINS with WHERE clause to get all records from one table and available records from the other

I am tryin to run a SQL query as below

SELECT organization.idorganization,
organization.name,
organization.image_url,
organization.website,
organization.description,
organization.phone,
organization.email,
organization.spare_parts_items,
organization.specialty_vehicles,
organization.on_the_spot_service,
organization.home_visits,
organization.latitude,
organization.longitude,
organization.no_of_views,
 organization.is_disabled,
organization.date_created,
organization.last_update,
organization.address,
cover_photo.idcover_photo,
cover_photo.image_url,
cover_photo.thumbnail,
( 6371 * acos( cos( radians(7.294324) ) * cos( radians( organization.latitude ) ) * cos( radians( organization.longitude ) - radians(80.646185) ) 
+ sin( radians(7.294324) ) * sin( radians( organization.latitude ) ) ) ) AS distance 
FROM organization 
LEFT OUTER JOIN cover_photo ON cover_photo.idcover_photo = organization.idorganization
WHERE organization.idorganization_type =1 HAVING distance < 20 AND cover_photo.thumbnail=true

Here, my expectation is to get all records from organization whether it has cover_photo s or not. Thats why I have used a LEFT OUTER JOIN . But, if it has cover_photo s, it should get only those which is a thumbnail . The cover_photo contains more than one photo for each organization .

When I run my code, I do not get what I am looking for. If there is no thumbnails available in cover_photo table, that particular organization is completely ignored. At this moment I have no cover_photo for any organization, so what I get is no results.

How to fix this?

Basically, you need to move the condition on the LEFT JOIN ed table from the WHERE clause to the ON clause of the LEFT JOIN : otherwise, it becomes mandatory, and filters out rows where the LEFT JOIN did not match.

That is, change this:

LEFT OUTER JOIN cover_photo 
    ON  cover_photo.idcover_photo = organization.idorganization
WHERE organization.idorganization_type = 1 AND cover_photo.thumbnail = true
HAVING distance < 20

To:

LEFT OUTER JOIN cover_photo 
    ON  cover_photo.idcover_photo = organization.idorganization
    AND cover_photo.thumbnail = true
WHERE organization.idorganization_type = 1 
HAVING distance < 20

Move AND cover_photo.thumbnail=true to ON clause expression:

... 
FROM organization  
LEFT OUTER JOIN cover_photo ON cover_photo.idcover_photo = organization.idorganization 
                           AND cover_photo.thumbnail=true 
WHERE organization.idorganization_type =1 
HAVING distance < 20 

The condition by a column from right table in LEFT JOIN (except checking this column for NULL directly or indirectly and null-safe compare) ejects all rows where left table have no matching row in right table. But the difference between INNER and LEFT joins is these left-without-right rows - ie the condition converts LEFT JOIN to INNER JOIN.

So this condition must be placed to ON clause - it will be checked/applied before joining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM