开发者

MYSQL Left Join Or Get value from Subquery if the value exists

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 cod开发者_运维问答e 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 ...?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜