简体   繁体   English

MySQL:使用带有 WHERE 子句的 JOINS 从一个表中获取所有记录并从另一个表中获取可用记录

[英]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 JOINON子句:否则,它变成强制性的,并过滤掉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.

相关问题 如何从一个表中获取所有记录,而从另一表中获取空 - How to get all records from one table and null with other table 使用WHERE mysql查询从主表中获取所有记录,并从子表中进行匹配 - Get all records from master table and matching from child using WHERE mysql query 使用Joins MySQL从表显示特定记录 - Display specific records from table using joins mysql 如何显示一个表中的所有记录,即使它们与JOIN不匹配WHERE子句 - How to show all records from one table even if they don't match WHERE clause with JOIN MySQL:如何从一个表中查找日期晚于其他表的日期的记录计数 - MySQL: How to find a count of records from one table where date is later than date from other table 如何在 where 子句和变量的帮助下从表中获取记录 - How to get records from a table with the help of a where clause and a variable 使用Where子句从Mysql数据库中获取多个记录 - Fetch Multiple Records from Mysql Database Using Where Clause Laravel - 从一个表中获取记录,该记录在另一个表中不存在,并附有 where 子句 - Laravel - Get records from one table that doesn't exist in another with a where clause attached MYSQL一个表中的所有记录,只有另一表中的最后一个连接的记录 - MYSQL All records from one table and only the last joined record from other table 使用 where IN 子句从多个表中删除记录 - Delete records From Multiple Table using where IN clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM