对于包含用户网页访问行为记录的表格,如何选择访问多个网页的用户。
这个表的结构是:
userId webpageId visitTime
0 123 ...
0 124 ...
1 123 ...
... ... ...
我可以使用:
SELECT userId, COUNT(DISTINCT webpageId) AS count FROM visits GROUP BY userId;
它给我的结果如下:
userId count
0 2
1 1
2 6
... ...
如何执行查询以获得最终结果,如:
userId
0
2
...
每个人都是访问多个DISTINCT网页的用户
答案 0 :(得分:34)
只需添加having子句
SELECT userId, COUNT(DISTINCT webpageId) AS count
FROM visits
GROUP BY userId
HAVING COUNT(DISTINCT webpageId) > 1
但如果你只是ID
SELECT userId
FROM visits
GROUP BY userId
HAVING COUNT(DISTINCT webpageId) > 1
您要对HAVING
子句而不是WHERE
进行过滤的原因是,WHERE
子句不支持聚合的列。
答案 1 :(得分:5)
试试这个:
SELECT userId, COUNT(DISTINCT webpageId) AS count FROM visits GROUP BY userId
having COUNT(DISTINCT webpageId) > 1
更多:HAVING
答案 2 :(得分:3)
虽然在这种情况下HAVING
是一种很好的方法,但请记住可以嵌套查询:
SELECT userId, pageCount
FROM (
SELECT userId, COUNT(DISTINCT webpageId) AS pageCount
FROM visits
GROUP BY userId) AS n
WHERE pageCount > 1
实际的查询计划可能会有所不同,尤其是HAVING
是优化案例,但没有理由说计划必须不同。 (比较特定RDBMS /版本的计划,如果它是一个问题或疑虑。)