table products
--------------
id | title
1 | Product 1
2 | Product 2
3 | Product 3
table categories
----------------
id | title
1 | Cat 1
2 | Cat 2
3 | Cat 3
table products_categories_mm
----------------
id_local | id_foriegn
1 | 2
1 | 3
2 | 3
3 | 2
SELECT *
FROM products
JOIN products_categories_mm ON products.uid=products_categories_mm.uid_local
JOIN categories ON products_categories_mm.uid_foreign = categories.uid
WHERE products.lang_uid IN (0)
AND products.page >= 1
AND products.page IN (8,9,10,11)
AND categories.uid IN (2,3)
ORDER BY products.datetime DESC
上面找到相關的類別2和3。它返回的產品記錄1,2和3這是精細的所有產品並按我的意願工作。但現在我需要調整查詢,以便它返回與類別2和類別3相關的產品。
在這種情況下,查詢應該返回產品1(因爲它是唯一與這兩個類別相關的產品)。
我試圖改變我的SQL語句:
SELECT *
FROM products
JOIN products_categories_mm ON products.uid=products_categories_mm.uid_local
JOIN categories ON products_categories_mm.uid_foreign = categories.uid
WHERE products.lang_uid IN (0)
AND products.page >= 1
AND products.page IN (8,9,10,11)
AND (categories.uid = 2 AND categories.uid = 3)
ORDER BY products.datetime DESC
但這沒有返回。 EXPLAIN
說有一個不可能的where子句。
如何更改我的SQL以獲取我想要的記錄?
你能舉個例子嗎?我從來沒有必須使用子查詢之前 –