简体   繁体   中英

Having problems writing mysql query joining 4 tables

I have a query (which partially works) looking like the following:

SELECT i.vehicle_id, i.user_id, i.make, i.model, i.year, i.state, i.price, i.class, i.fuel, i.mileage, i.mileage_type, i.featured, i.published, i.creation, i.status, m.vehicle_id, m.type, m.src, a.user_id, a.inventory_status, a.country, s.user_id, s.currency_iso, s.currency_code 
FROM `cms_dealer_inventory` AS `i`, 
`cms_dealer_inventory_media` AS `m`, 
`cms_dealer_account` AS `a`, 
`cms_dealer_setting` AS `s` 
WHERE i.make='chevrolet' 
AND i.model='camaro' 
AND i.year='2014' 
AND (i.mileage <= 200000 ) 
AND i.published='1' 
AND a.inventory_status='1' 
AND m.vehicle_id=i.vehicle_id 
AND m.type='exterior' 
AND a.user_id=i.user_id 
AND s.user_id=i.user_id 
AND a.country='DE' 
GROUP BY i.vehicle_id 
ORDER BY i.featured DESC, i.price ASC, i.state DESC, i.make ASC

The problem is, the cms_dealer_inventory_media may or may not contain images (bind by vehicle_id). I know the problem is with my WHERE statement in which I am specifically saying m.vehicle_id=i.vehicle_id AND m.type='exterior' but, it causes entries with no images to be ignore. What I need is a query which makes the m.vehicle_id=i.vehicle_id run IF there are results to cms_dealer_inventory_media .

If you change your query to use the more modern explicit joins , you could use left join s:

SELECT i.vehicle_id, i.user_id, i.make, i.model, i.year, i.state, i.price, i.class, i.fuel, i.mileage, i.mileage_type, i.featured, i.published, i.creation, i.status, m.vehicle_id, m.type, m.src, a.user_id, a.inventory_status, a.country, s.user_id, s.currency_iso, s.currency_code 
FROM `cms_dealer_inventory` AS `i`
LEFT JOIN `cms_dealer_inventory_media` AS `m` ON m.vehicle_id=i.vehicle_id 
LEFT JOIN `cms_dealer_account` AS `a` ON a.user_id=i.user_id 
LEFT JOIN `cms_dealer_setting` AS `s` ON s.user_id=i.user_id 
WHERE i.make='chevrolet' 
AND i.model='camaro' 
AND i.year='2014' 
AND (i.mileage <= 200000 ) 
AND i.published='1' 
AND a.inventory_status='1' 
AND m.type='exterior' 
AND a.country='DE' 
GROUP BY i.vehicle_id 
ORDER BY i.featured DESC, i.price ASC, i.state DESC, i.make ASC

You should use left join

SELECT i.vehicle_id, i.user_id, i.make, i.model, i.year, i.state, i.price, i.class, i.fuel, i.mileage, i.mileage_type, i.featured, i.published, i.creation, i.status, m.vehicle_id, m.type, m.src, a.user_id, a.inventory_status, a.country, s.user_id, s.currency_iso, s.currency_code 
FROM `cms_dealer_inventory` AS `i` left join
`cms_dealer_inventory_media` AS `m` on m.vehicle_id=i.vehicle_id 
AND m.type='exterior' left join
`cms_dealer_account` AS `a` on a.inventory_status='1' 
AND a.user_id=i.user_id AND a.country='DE' left join
`cms_dealer_setting` AS `s` ON s.user_id=i.user_id  

WHERE i.make='chevrolet' 
AND i.model='camaro' 
AND i.year='2014' 
AND (i.mileage <= 200000 ) 
AND i.published='1' 
GROUP BY i.vehicle_id 
ORDER BY i.featured DESC, i.price ASC, i.state DESC, i.make ASC

you will have to use the join properly

in your case you need all the records from cms_dealer_inventory

so join will be cms_dealer_inventory left outer cms_dealer_inventory_media

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM