[英]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我正在尝试运行 SQL 查询,如下所示
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.在这里,我的期望是从organization
中获取所有记录,无论它是否有cover_photo
s。 Thats why I have used a LEFT OUTER JOIN
.这就是我使用LEFT OUTER JOIN
的原因。 But, if it has cover_photo
s, it should get only those which is a thumbnail
.但是,如果它有cover_photo
s,它应该只获得thumbnail
。 The cover_photo
contains more than one photo for each organization
. cover_photo
包含每个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.如果cover_photo
表中没有可用的thumbnails
,则该特定organization
将被完全忽略。 At this moment I have no cover_photo
for any organization, so what I get is no results.目前我没有任何组织的cover_photo
,所以我得到的是没有结果。
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.基本上,您需要将LEFT JOIN
ed 表上的条件从WHERE
子句移动到 LEFT LEFT JOIN
的ON
子句:否则,它变成强制性的,并过滤掉LEFT JOIN
不匹配的行。
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:将AND cover_photo.thumbnail=true
移动到ON
子句表达式:
...
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. LEFT JOIN 中右表中的一列的条件(除了直接或间接检查此列是否为 NULL 和空安全比较)弹出左表在右表中没有匹配行的所有行。 But the difference between INNER and LEFT joins is these left-without-right rows - ie the condition converts LEFT JOIN to INNER JOIN.但是 INNER 和 LEFT 连接之间的区别在于这些左行没有右行 - 即条件将 LEFT JOIN 转换为 INNER JOIN。
So this condition must be placed to ON clause - it will be checked/applied before joining.所以这个条件必须放在 ON 子句中——它会在加入之前被检查/应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.