我有一个表格示例http://prntscr.com/msqpep 我在做
时需要空结果SELECT * FROM prods
WHERE prod_id = 3 and (fet_id = 1 and fet_id = 5)
但该代码(fet_id = 1 and fet_id = 5)
或(fet_id = 1 and fet_id = 3)
不起作用
"Where in"
是最好的方法,但是我需要`WHERE IN(1 and 2 and 3)
默认情况下,"WHERE IN"
以(1 or 2 or 3)
的身份工作
当我选择fet_id = 1和fet_id = 3时,我需要两个结果 当我选择fet_id = 1和fet_id = 5时我不需要结果
答案 0 :(得分:1)
您可能想要
SELECT prod_id FROM prods GROUP by prod_id
HAVING (SUM(fet_id=1)>0
AND SUM(fet_id=5)>0)
OR
(SUM(fet_id=1)>0
AND SUM(fet_id=3)>0)
HAVING过滤器将WHERE过滤器组分组。
答案 1 :(得分:1)
尝试一下:
SELECT p1.id AS id1, p2.id AS id2
FROM `prods` p1 LEFT JOIN `prods` p2 ON p1.`prod_id` = p2.`prod_id`
WHERE p1.`fet_id` = '1' AND p2.`fet_id` = '3'
输出
id1 id2
1 3
输入fet_id = 1和fet_id = 5
SELECT p1.id AS id1, p2.id AS id2
FROM `prods` p1 LEFT JOIN `prods` p2 ON p1.`prod_id` = p2.`prod_id`
WHERE p1.`fet_id` = '1' AND p2.`fet_id` = '5'
输出:没有结果
答案 2 :(得分:0)
如果需要两个以上的值,则另外一个
SELECT res.* FROM (SELECT 3 as total,
(SELECT COUNT(*) FROM `prods` as t1 WHERE t1.`fet_id` = 1 OR t1.`fet_id` = 2 OR t1.`fet_id` = 5) as current,
t2.*
FROM `prods` as t2 WHERE t2.`fet_id` in (1,2,5)) as res WHERE res.total = res.current