使用以下查询记录时出现问题
SELECT user_id FROM temp
WHERE
(value = 1 AND field_id = 11) AND
value = 1 AND field_id = 12
这是桌子。
我应该获得101个user_id的记录。
对此有任何想法吗?
答案 0 :(得分:0)
您需要按用户聚合:
SELECT user_id
FROM temp
GROUP BY user_id
HAVING
SUM(CASE WHEN value = 1 AND field_id = 11 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN value = 1 AND field_id = 12 THEN 1 ELSE 0 END) > 0;
答案 1 :(得分:0)
尝试此sql从temp中选择DISTINCT user_id,其中value = 1和(field_id = 12或field_id = 11)
答案 2 :(得分:0)
您还可以使用where
子句:
select user_id
from table t
where value = 1 and field_id in (11, 12)
group by user_id
having count(distinct field_id) = 2;