我有一个这样的表sql
post_id point user_id
0 1.5 1
1 1.0 1
2 1.5 2
3 0.0 3
4 1.5 4
5 1.0 1
6 1.0 2
7 1.5 1
8 0.0 2
9 1.5 3
根据上表:
如何找出哪些用户的帖子少于表中帖子最多的用户,然后像这样更新表
post_id point user_id
0 1.5 1
1 1.0 1
2 1.5 2
3 0.0 3
4 1.5 4
5 1.0 1
6 1.0 2
7 1.5 1
8 0.0 2
9 1.5 3
NULL NULL 2
NULL NULL 3
NULL NULL 3
NULL NULL 4
NULL NULL 4
NULL NULL 4
感谢您的帮助。
答案 0 :(得分:2)
尝试一下,可以帮助您:
SELECT user_id, GROUP_CONCAT(post_id) as postIds, SUM(point) as totalPoints,
count(user_id) as totalPosts ,
((SELECT count(user_id) as maxtotal FROM `posts` GROUP BY posts.user_id ORDER BY maxtotal DESC LIMIT 1) - count(user_id)) as totalRemaining
FROM `posts`
GROUP BY posts.user_id
HAVING totalPosts < (SELECT count(user_id) as maxtotal FROM `posts`
GROUP BY posts.user_id ORDER BY maxtotal DESC LIMIT 1)
ORDER BY totalPosts DESC