我有表cities
與colums id
,name
,region_id
和表orders
與colums id
,city_id
。MySQL的存在,或者JOIN
我需要從orders
中選擇所有行,需要的地方region_id
。
隨着EXISTS
:
SELECT `o`.`id`
FROM `orders` as `o`
WHERE EXISTS (SELECT `id`
FROM `cities`
WHERE `id` = `o`.`city_id` AND `region_id` = ".$region_id.")
隨着JOIN
:
SELECT `o`.`id`
FROM `orders` as `o`
LEFT JOIN `cities` as `c` ON `o`.`city_id` = `c`.`id`
WHERE `c`.`region_id` = ".$region_id."
什麼是在這種情況下更好?
你自己的測試揭示了什麼? – Strawberry
我會測試它。但我認爲在這個問題上有一些共同的意見 –