我想在以下SQL查詢中使用JOIN而不是IN。我無法弄清楚如何去做。使用JOIN sql查詢
SELECT * FROM shop_orders WHERE
id IN (SELECT orders_id FROM shop_orders_data WHERE closed='1' /*AND backorder='0'*/ AND exhibition_id='389' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE
country_id IN (SELECT id FROM countries WHERE id='72')) AND in_country = '72' AND
exhibition_id IN (SELECT id FROM shop_exhibitions WHERE start<=1336946400 AND end>1336600800)) AND
id IN (SELECT orders_id FROM shop_orders_products WHERE
products_id IN (SELECT id FROM shop_products WHERE artno='120000' OR name LIKE '%120000%')) AND created>=1333231200 AND created<1333663200 ORDER BY created DESC
我嘗試這樣做:
SELECT
s.*
FROM
shop_orders s
INNER JOIN shop_orders_data od ON s.id=od.orders_id
INNER JOIN shop_exhibitions se ON od.exhibition_id=se.id
INNER JOIN countries co ON se.country_id=co.id
INNER JOIN shop_orders_products sop ON s.id=sop.orders_id
INNER JOIN shop_products sp
ON sop.products_id=sp.id
WHERE od.closed=1
AND (sp.artno='120000' or sp.name LIKE '%120000%')
AND (od.exhibition_id='389')
AND (od.in_country = '72')
AND (se.start <=1336946400)
AND (se.end >1336600800)
AND (se.created>=1333231200)
AND (se.created<1333663200)
ORDER BY `s`.`created` DESC
我這是否正確?
你有沒有考慮閱讀第一聯接語法的文檔? – JohnFx