我有下表,我想选择
的所有用户我知道我错了,但我该怎样编辑我的查询?
表格
user --- type--- type_id
abc ---- form --- 3
abc ---- action--- 5
abc ---- action--- 3
abc ---- form --- 6
当前查询
Select user
from table
where type = 'form' type = 'action'
and type_id IN(3, 5)
Group
By user
Having count(1) = 2;
答案 0 :(得分:1)
您只需将type
和type_id
的条件配对并使用OR:
SELECT user FROM table
WHERE (type = 'form' AND type_id = 3)
OR (type = 'action' AND type_id = 5)
GROUP BY user HAVING count(1) = 2;
由于某种原因,我没有黄金[mysql]或[sql]徽章(我不知道HAVING count(1) = 2
是什么)。关闭以查看它。
答案 1 :(得分:1)
以下查询将提供您在上面提到的条件的所有用户名。
SELECT t1.user FROM table_name t1 inner join table_name t2
WHERE (t1.type = 'form' AND t1.type_id = 3) and
(t2.type = 'action' AND t2.type_id = 5)
GROUP BY t1.user;