我有一张这样的table:
---------------------------------------
| Actions |
---------------------------------------
| action_id | user_id | action_active |
---------------------------------------
| 1 | 1 | 0 |
---------------------------------------
| 2 | 2 | 1 |
---------------------------------------
| 3 | 1 | 0 |
---------------------------------------
| 4 | 2 | 0 |
---------------------------------------
我要检索用户将其所有行作为action\u active=0的所有行。如果他只有一个动作被激活为1,不要检索它。在本例中,它应该只检索第1行和第3行,因为用户1的所有行都处于action\u active=0。
我想过这样的事情,但我不确定这是否正确:
SELECT *
FROM Actions AS a
WHERE action_active = ALL (SELECT action_active FROM actions as s WHERE action_active = 0 where a.idx_user = s.idx_user)
我不确定我的问题是否正确。
谢谢!
2条答案
按热度按时间jgzswidk1#
在子查询中计算sum以查找所有零值的用户,并将其与main select连接起来
cngwdvgl2#
使用
NOT EXISTS
:这应该比使用
group by
--这就直接使用了actions(user_id, action_active)
.您也可以使用
LEFT JOIN
: