I'm stuck, I tried to grab statistics from DB.
My query needs to return count of users that at least have 1 entry in connections table.
So I tried:
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;
This returns correct user_id, I've 3 rows with correct id which is all fine.
But when I do count(u.id) it returns 35 which instead should be 3. My understanding is it is counting non DISTINCT number of rows. So what should I do?
And as a last part of my question, how do I unite this with other stat queries of mine?
/*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;*/
Testing playground: http://www.sqlfiddle.com/#!2/c79a6/63
SELECT COUNT(DISTINCT user_id) FROM connections
for the first part :
/* 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.