我有一个表格结构如下:
id int
userid int
status enum ('pending','approved')
dop datetime
数据如下:
id userid status dop
1 24 pending 2011-02-14 06:41:32
2 24 pending 2011-02-15 23:02:45
当我触发以下查询时:
SELECT count( userid )
FROM t1
WHERE STATUS = 'pending'
GROUP BY userid
它给我算作'2',这是错的,谁能告诉我这里有什么问题?以及如何将实数计为1
答案 0 :(得分:5)
group by语句在计数后执行。请改用:
SELECT count( DISTINCT userid )
FROM t1
WHERE STATUS = 'pending'
答案 1 :(得分:1)
您是否要计算状态为暂挂的用户数?
SELECT count(userid)
FROM t1
WHERE STATUS = 'pending'
GROUP BY status, userid
答案 2 :(得分:0)
尝试在select子句中添加userid:
SELECT userid, count( userid )
FROM t1
WHERE STATUS = 'pending'
GROUP BY userid
答案 3 :(得分:0)
可能在userid上添加DISTINCT()?
答案 4 :(得分:0)
SELECT count( DISTINCT userid )
FROM t1
WHERE STATUS = 'pending'
GROUP BY userid
答案 5 :(得分:0)
你应该使用COUNT(DISTINCT())构造,它允许你计算不同的值而不是NULL(docu)
试试这句话:
SELECT count( DISTINCT( userid ) )
FROM t1
WHERE STATUS = 'pending'
GROUP BY userid
HTH!