使用distinct优化mysql查询

bttbmeg0  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(286)

我想找到每天通过通知访问应用程序的用户数。我写的查询很慢。

SELECT DATE(user_statistics.timestamp) date, COUNT(*) count FROM post
INNER JOIN user_statistics ON user_statistics.post_id = post.id
AND user_statistics.access_type = 'notification' AND post.type = 'given_type'
GROUP BY DATE(user_statistics.timestamp)

我重写了它,用 DISTINCT .

SELECT DISTINCT date(user_statistics.timestamp), count(DISTINCT user_statistics.post_id) 
FROM user_statistics INNER JOIN post on post.id = user_statistics.post_id
WHERE post.type = 'given_type' AND access_type = 'notification'

新查询不起作用。上面写着
在没有分组依据的聚合查询中,select list的表达式#1包含未聚合的列“user#u statistics.timestamp”;这与sql\u mode=only\u full\u group by不兼容

vwhgwdsa

vwhgwdsa1#

您忘记添加group by子句:

SELECT DISTINCT date(user_statistics.timestamp), count(DISTINCT user_statistics.post_id) 
FROM user_statistics INNER JOIN post on post.id = user_statistics.post_id
WHERE post.type = 'given_type' AND access_type = 'notification'
group by date(user_statistics.timestamp)

相关问题