我有一個表格幫助選擇產品。具有多個AND或WHERE條件的複雜MySQL查詢
我想有一個查詢,我回來的每個記錄其中:
category_id = 'M10' and band_id = '010'
OR
category_id = 'M10' and band_id = '020'
AND
category_id = 'M50' and band_id = '010'
OR
category_id = 'M50' and band_id = '020'
我的查詢:
SELECT
product.entry_id AS entry_id,
helpme.item_id AS product_id
FROM inbound_help_me_choose helpme
LEFT JOIN exp_channel_titles product ON (product.product_id = helpme.item_id)
WHERE
helpme.submarket_id = 'NZ'
AND
(
(helpme.category_id = 'M10' AND helpme.band_id = '010')
OR
(helpme.category_id = 'M10' AND helpme.band_id = '020')
)
正常工作的第一類,但只要我添加新的WHERE條件失敗:
SELECT
product.entry_id AS entry_id,
helpme.item_id AS product_id
FROM inbound_help_me_choose helpme
LEFT JOIN exp_channel_titles product ON (product.product_id = helpme.item_id)
WHERE
helpme.submarket_id = 'NZ'
AND
(
(helpme.category_id = 'M10' AND helpme.band_id = '010')
OR
(helpme.category_id = 'M10' AND helpme.band_id = '020')
)
AND
(
(helpme.category_id = 'M50' AND helpme.band_id = '010')
OR
(helpme.category_id = 'M50' AND helpme.band_id = '020')
OR
(helpme.category_id = 'M50' AND helpme.band_id = '030')
)
對我來說這似乎沒問題,所以我完全失去了爲什麼它不工作。
即使每個人都知道ANDs在條件邏輯中的ORs之前獲得了先行優先權,但我總是建議使用括號來澄清,特別是一旦您開始獲得條款中的兩個或三個條件。 – Ray