Count unique rows mysql(使用返回非不同值的Count)

Count unique rows mysql(使用返回非不同值的Count),mysql,sql,Mysql,Sql,我被卡住了,我试图从数据库中获取统计数据 我的查询需要返回连接表中至少有1个条目的用户计数 所以我试着: SELECT DISTINCT (u.id) AS total_has_atleast_one_word FROM users AS u LEFT JOIN connections AS c ON u.id = c.user_id WHERE c.word_id IS NOT null; 这将返回正确的用户id,我有3行正确的id,这一切都很好 但当我计算u.id时,它返回35,应该是3。

我被卡住了,我试图从数据库中获取统计数据

我的查询需要返回连接表中至少有1个条目的用户计数

所以我试着:

SELECT DISTINCT (u.id) AS total_has_atleast_one_word FROM users AS u
LEFT JOIN connections AS c
ON u.id = c.user_id
WHERE c.word_id IS NOT null;
这将返回正确的用户id,我有3行正确的id,这一切都很好

但当我计算u.id时,它返回35,应该是3。我的理解是,这是在计算不明显的行数。那我该怎么办

作为我问题的最后一部分,我如何将其与我的其他统计查询结合起来

/*SELECT COUNT(u.id) AS total_users,
       sum(u.created < (NOW() - INTERVAL 7 DAY)) as total_seven_day_period,
       sum(u.verified = 1) as total_verified,
       sum(u.level = 3) as total_passed_tut,
       sum(u.avatar IS NOT null) as total_with_avatar,
       sum(u.privacy = 0) as total_private,
       sum(u.privacy = 2) as total_friends_only,
       sum(u.privacy = 3) as total_public,
       sum(u.sex = "F") as total_female,
       sum(u.sex = "M") as total_male
FROM users AS u;*/
测试场:

第一部分:

/* user ids of those who have ever connected */
Select user_id from connections group by user_id

/* to get those who have connected after a particular date time ... */
Select user_id from connections group by user_id where connection_time > '2013-11-23' 

/* join with user table to get user details e.g. .. */   
Select u.name , u.address from user u
join on (Select user_id from connections group by user_id) c on c.user_id =u.user_id

这个查询也有同样的问题。是的,这更简单,但我仍然需要将它与我的第二个查询结合起来。给出示例数据、预期结果和实际结果。给你:应该返回4。因为有4个用户在连接表中至少有一个单词。我写这个答案的时候一定是睡着了。修正了,所以我把它当作子查询?选择COUNTDISTINCT user\u id FROM connections作为总计\u至少\u一个单词
/* user ids of those who have ever connected */
Select user_id from connections group by user_id

/* to get those who have connected after a particular date time ... */
Select user_id from connections group by user_id where connection_time > '2013-11-23' 

/* join with user table to get user details e.g. .. */   
Select u.name , u.address from user u
join on (Select user_id from connections group by user_id) c on c.user_id =u.user_id