我有一个这样的用户表
user_id | community_id | registration_date
--------------------------------------------
1 | 1 | 2008-01-01
2 | 1 | 2008-05-01
3 | 2 | 2008-01-28
4 | 2 | 2008-07-22
5 | 3 | 2008-01-11对于每个社区,我想得到第三个用户注册的时间。使用MySql的“限制”SQL扩展,我可以很容易地为单个社区做到这一点。例如,对于使用ID=2的社区
select registration_date
from user
order by registration_date
where community_id = 2
limit 2, 1或者,我可以通过以下方式获得第一个用户为所有社区注册的日期:
select community_id, min(registration_date)
from user
group by 1但是我不知道如何在一个SQL语句中获得所有社区的第三个用户的注册日期。
干杯,唐
发布于 2008-12-08 16:10:49
具有内部选择:
select
registration_date, community_id
from
user outer
where
user_id IN (
select
user_id
from
user inner
where
inner.community_id = outer.community_id
order by
registration_date
limit 2,1
)
order by registration_date选择用户集合,其中每个用户是其社区中的第三个用户,由内部select中的限制子句返回。
发布于 2008-12-08 16:11:35
你是这个意思吗?
SELECT registration_date
FROM user
ORDER BY registration_date
LIMIT n其中n是您所关心的用户。
https://stackoverflow.com/questions/349933
复制相似问题