MYSQL Left Join 或从子查询获取值(如果值存在)

发布于 2024-12-10 05:41:47 字数 1087 浏览 2 评论 0原文

我有下面的查询,它工作完美,没有问题

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-12-17 05:41:47

您可以再次左连接其余表car_code,从而使 car_code 成为主导查询。
或者将 car_code 右连接到当前查询,本质上具有相同的效果,但我建议不要混合左连接和右连接。

问题是你总是需要一个 car_code ,否则你将不会得到其他数据(你现在遇到的问题相同,但反过来),但当我阅读你的要求时,这不应该是一个问题:

我强烈建议您在表之间使用更一致的字段名称,并使用 INNER 和 LEFT 连接以及表别名以提高可读性。您的查询可能如下所示,尽管它并不完整,因为我不知道表之间的确切关系。

   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 car_code cc
LEFT JOIN my_area_details ad ON ad.code = cc.car_code 
                            AND ad.type = 4 
LEFT JOIN my_user u ON u.user_id = ad.created_by
LEFT JOIN my_master_area_categories mac ON mac.type_id = ad.category
    WHERE car_code.car_company = 'GM'
      AND car_code.car_model = 'Chevy'    
 ORDER BY category

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.

   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 car_code cc
LEFT JOIN my_area_details ad ON ad.code = cc.car_code 
                            AND ad.type = 4 
LEFT JOIN my_user u ON u.user_id = ad.created_by
LEFT JOIN my_master_area_categories mac ON mac.type_id = ad.category
    WHERE car_code.car_company = 'GM'
      AND car_code.car_model = 'Chevy'    
 ORDER BY category
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文