MYSQL Left Join 或从子查询获取值(如果值存在)
我有下面的查询,它工作完美,没有问题
SELECT description category,
details,
created_by,
CONCAT(name,' ',surname) name,
FROM_UNIXTIME(createdon) created_date,
FROM_UNIXTIME(updatedon) updated_date,
comments_count,
entry_id,
code car_code
FROM my_area_details,my_user,my_master_area_categories
WHERE type=4
AND code IN ( SELECT car_code
FROM my_cars
WHERE car_company='GM'
AND car_model='Chevy')
AND my_area_details.created_by=my_user.user_id
AND my_area_details.category=my_master_area_categories.type_id
ORDER BY category
在上面的查询中,下面的子查询仅返回一个值或什么都不返回,
SELECT car_code
FROM my_cars
WHERE car_company='GM'
AND car_model='Chevy'
在第一个查询中,如果我的其他表没有任何匹配的记录,我会得到零条记录。
我的要求是,如果子查询有值并且主查询没有返回任何记录(由于在其他表中找不到记录),我至少应该从 my_cars 中获取 car_code 的值(其他值可以为 NULL)。
为此,我尝试添加子查询作为别名表,并与该别名表进行左连接, 但由于我有其他条件,我仍然没有获得汽车代码。还尝试在其他三个表上进行连接,结果与 my_car 表进行左连接。但这需要时间,因为我没有在第一级查询中检查汽车代码
即使我的其他表没有任何匹配的内容,有什么方法可以加入并从 my_cars 表中获取 car_code ...?
I have the below query and it works perfect , no issues
SELECT description category,
details,
created_by,
CONCAT(name,' ',surname) name,
FROM_UNIXTIME(createdon) created_date,
FROM_UNIXTIME(updatedon) updated_date,
comments_count,
entry_id,
code car_code
FROM my_area_details,my_user,my_master_area_categories
WHERE type=4
AND code IN ( SELECT car_code
FROM my_cars
WHERE car_company='GM'
AND car_model='Chevy')
AND my_area_details.created_by=my_user.user_id
AND my_area_details.category=my_master_area_categories.type_id
ORDER BY category
In the above query the below sub query returns only one value or nothing
SELECT car_code
FROM my_cars
WHERE car_company='GM'
AND car_model='Chevy'
In First query if my other tables doesn't have any matching records I get zero records .
My requirement is if the the sub query has value and the main query not returning any records ( due to records not found in other tables) , I should at least get the value of car_code from my_cars (Other values can be NULL).
For that I tried adding subquery as alias table and did left join with that alias table ,
But since I have other conditions I am still not getting car code .Also tried doing joining on the other three table and with the result doing the left join with my_car table . But this one taking time since I am not checking against car code in the first level query
Any way to Join and get the car_code from my_cars table even if my other tables doesn't have any thing matching ...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以再次左连接其余表
car_code
,从而使 car_code 成为主导查询。或者将
car_code
右连接到当前查询,本质上具有相同的效果,但我建议不要混合左连接和右连接。问题是你总是需要一个 car_code ,否则你将不会得到其他数据(你现在遇到的问题相同,但反过来),但当我阅读你的要求时,这不应该是一个问题:
我强烈建议您在表之间使用更一致的字段名称,并使用 INNER 和 LEFT 连接以及表别名以提高可读性。您的查询可能如下所示,尽管它并不完整,因为我不知道表之间的确切关系。
You can left join the rest of the tables agains
car_code
, thus making car_code the leading query.Or right join
car_code
to your current query, which essentially has the same effect, but I would recomment not to mix left join and right join.Problem is that you will always need a car_code, or else you won't get other data back (same problem you got now, but the other way around), but as I read your requirements, this should not be a problem:
I strongly advise you to use more consistent field names across tables, and to use INNER and LEFT joins, and table aliasing for readability. Your query could look like this, although it's not complete since I don't know the exact relations between your tables.