如果我有一个三列的表
用户 ID,事件 ID,进入时间
和价值观
userid,eventid,entrytime
1 1 NULL
2 1 2012-01-01
1 2 2012-01-01
2 2 2012-01-01
3 1 NULL
3 2 NULL
选择 entrytime 始终为 NULL 的用户 ID 的最佳方法是什么
由于聚合将消除 NULL 值,因此您可以通过将其与子句中的 0 进行比较COUNT(entrytime)
来确定哪个userid
没有非空值。entrytime
HAVING
SELECT userid, COUNT(entrytime)
FROM yourtable
GROUP BY userid
HAVING COUNT(entrytime) = 0
这是一个现场演示(结果为userid
= 3)
试试这个:
select userid from
(select userid,entrytime,count(*) from yourtable
group by userid,entrytime)a
group by userid
having count(*)=1 and max(entrytime) is null
SELECT DISTINCT t1.UserId
FROM <TableName> t1
LEFT JOIN <TableName> t2 ON t1.UserID = t2.UserID AND t2.EntryTime IS NOT NULL
WHERE t2.UserID IS NULL
select userid from table_name where entrytime is null