我需要为每个不同的记录选择几行。虽然我使用的是MySQL,但这里的问题很多Select first n records for each distinct ID in SQL Server 2008。
目的可以通过在这种情况下运行21个查询来实现:1个通用,20个用于获取子记录,例如:
SELECT DISTINCT `user_id`
FROM `posts`
WHERE `deleted` = '0'
ORDER BY `user_id` ASC
LIMIT 20
...选择所需的所有行,然后
SELECT *
FROM `posts`
WHERE `deleted` = '0'
AND `user_id` = ?
ORDER BY `id` DESC
LIMIT 5
...在第一个查询选择的每一行的循环中。
基本上,我需要每个用户获得5个帖子。我需要在一个查询中完成此操作。 posts
设置就是例如,我做了这个,所以希望更容易理解我需要的东西。
我开始使用以下查询:
SELECT *
FROM `posts`
WHERE `user_id`
IN (
SELECT DISTINCT `user_id`
FROM `posts`
ORDER BY `user_id` DESC
LIMIT 4
)
LIMIT 5
但我收到#1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
错误。
所以我尝试过像here建议的JOIN
想法:
SELECT posts.id,
posts.user_id,
NULL
FROM (
SELECT posts.user_id
FROM posts
WHERE posts.deleted = 0
LIMIT 20
) q
JOIN posts
ON posts.user_id = q.user_id
我还尝试了几个嵌套查询here:
SELECT *
FROM posts
WHERE user_id IN (
SELECT * FROM (
SELECT user_id
FROM posts
LIMIT 20
)
as t);
在互联网上找到的其他解决方案。但它们要么不起作用,要么只是简单地从数据库中选择前N行(无论条件和连接由于某种原因)。尝试LEFT JOIN
,RIGHT JOIN
,甚至INNER JOIN
,但仍然没有成功。
请帮忙。
更新忘记提及该表的大小约为5GB。
更新 尝试了子查询:
SELECT *
FROM `posts`
WHERE
`user_id` IN ( SELECT `user_id` FROM (
SELECT DISTINCT `user_id`
FROM `posts`
ORDER BY `user_id` DESC
LIMIT 4 ) limit_users
)
LIMIT 5
与上面相同,它返回以下内容:
+----+---------+------+
| id | user_id | post |
+----+---------+------+
| 1 | 1 | a |
+----+---------+------+
| 2 | 1 | b |
+----+---------+------+
| 3 | 1 | c |
+----+---------+------+
| .. | .. | .. |
即。同一用户的5(这是外部LIMIT
设置的)行。奇怪的是,如果我单独运行子和子子查询:
SELECT `user_id` FROM (
SELECT DISTINCT `user_id`
FROM `posts`
ORDER BY `user_id` DESC
LIMIT 4 ) limit_users
我得到了4个不同的值:
+---------+
| user_id |
+---------+
| 1 |
+---------+
| 2 |
+---------+
| 3 |
+---------+
| 4 |
+---------+
答案 0 :(得分:1)
您必须使用变量,对有序查询执行两个不同的计数:一个针对每个用户的帖子数量,另一个针对用户:
SELECT posts_counts.*
FROM (
SELECT
posts.*,
@post_count:=case when @prec_user_id=user_id then @post_count+1 else 1 end as pc,
case when @prec_user_id<>user_id then @user_count:=@user_count+1 else @user_count end as uc,
@prec_user_id:=user_id
FROM
posts,
(select @prec_user_id:=0, @user_count:=0, @post_count:=0) counts
ORDER BY
posts.user_id ) posts_counts
WHERE pc<5 and uc<4
编辑:您也可以考虑尝试此查询:
SELECT *
FROM `posts`
WHERE
`user_id` IN ( SELECT user_id FROM (
SELECT DISTINCT `user_id`
FROM `posts`
ORDER BY `user_id` DESC
LIMIT 4 ) limit_users
)
LIMIT 5
(这将从每个选定用户的所有帖子中选择5个帖子,因此它仍然不是您需要的,但它使用技巧在子子查询中使用LIMIT)
EDIT2:下一个查询将限制20个用户中的每个用户的5个帖子:
select posts_limited.*
from (
select
posts.*,
@row:=if(@last_user=posts.user_id, @row+1, 1) as row,
@last_user:=posts.user_id
from
posts inner join
(select user_id from
(select distinct user_id
from posts
order by user_id desc
LIMIT 20) limit_users
) limit_users
on posts.user_id = limit_users.user_id,
(select @last_user:=0, @row:=0) r
) posts_limited
where row<=5