我正在嘗試爲表(公寓)創建過濾器,並通過apartsments_features
表與apartment features
建立多對多關係。在子查詢中的MySQL交集
我想只包括全部的某些功能(在窗體上標記爲「是」)的公寓,但不包括具有任何其他功能(標記爲「否」)的公寓。我意識到太遲了,我不能在MySQL中使用INTERSECT
或MINUS
。
我有一個查詢,看起來像:
SELECT `apartments`.* FROM `apartments` WHERE `apartments`.`id` IN (
SELECT `apartments`.`id` FROM `apartments` INTERSECT (
SELECT `apartment_id` FROM `apartments_features` WHERE `feature_id` = 103
INTERSECT SELECT `apartment_id` FROM `apartments_features` WHERE `feature_id` = 106
) MINUS (
SELECT `apartment_id` FROM `apartments_features` WHERE `feature_id` = 105 UNION
SELECT `apartment_id` FROM `apartments_features` WHERE `feature_id` = 107)
)
ORDER BY `apartments`.`name` ASC
我敢肯定有辦法做到這一點,但此刻我的知識僅限於較簡單的左更小,右連接。
是的,這是一個錯字。 – Zahymaka 2010-09-21 21:47:50