查詢將返回唯一1的結果,請大家幫忙SQL查詢只返回1個結果
select
p.products_id, pd.products_name
from
products_description pd,
products_to_categories p2c,
products p
where
EXISTS (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE p2pef.products_extra_fields_id = '78'
and p2pef.products_extra_fields_value = 'tak'
and p.products_id = p2pef.products_id)
and
EXISTS (SELECT p2pef.products_id
FROM products_to_products_extra_fields p2pef
WHERE p2pef.products_extra_fields_id = '94'
and p2pef.products_extra_fields_value = 'tak'
and p.products_id = p2pef.products_id)
and p.products_status = '1'
and p.products_id = pd.products_id
and p.products_id = p2c.products_id
and pd.language_id = '4'
and p2c.categories_id = '26'
group by p.products_id
你認爲怎麼樣使用EXISTS此查詢?
您可能有另一種想法來顯示來自products_to_products_extra_fields表的其他字段滿足多個條件的產品表中的產品。
表模式:
產品(4行)
_________________________
products_id
_________________________
1
2
3
4
products_to_products_extra_fields(8行)
___________________________________________________________________
products_id products_extra_fields_id products_extra_fields_value
___________________________________________________________________
1 94 tak
1 78 tak
2 94 tak
2 78 tak
3 94 tak
3 78 tak
4 94 nie
4 78 tak
有效的查詢應該返回(3行)
_______________
products_id
_______________
1
2
3
'EXISTS'部分似乎沒問題。問題可能是您的其他過濾器只選擇了一個'product_id' –
的確,所有* product_ids *都不帶'EXISTS'子句? – Parfait
嗨。 「滿足幾個條件」是什麼意思?您似乎對輸入「條件」的某種表示形式和輸出查詢感興趣,但您沒有足夠詳細地解釋我們可以知道什麼是正確的答案。請同時閱讀[mcve]並採取行動。事實上,你的問題到底是什麼? – philipxy