我被困住了,我试图从数据库中获取统计数据。
我的查询需要返回至少在连接表中有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;
这会返回正确的user_id,我有3行,ID正确,这一切都很好。
但是,当我计算(u.id)时,它返回35,而应该是3.我的理解是它计算非DISTINCT行数。那我该怎么办?
作为我问题的最后一部分,我如何将其与我的其他统计查询联合起来?
/*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;*/
答案 0 :(得分:0)
SELECT COUNT(DISTINCT user_id) FROM connections
答案 1 :(得分:0)
第一部分:
/* 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