mysql:如何限制查询?

时间:2012-05-29 09:32:04

标签: mysql

我选择记录并按日期和uid(不是主键)对它们进行分组,我需要按日期对这些记录进行分页,我该怎么办呢?
例如:

uid  date
1    2012-01-10
2    2012-01-10
3    2012-01-09
3    2012-01-09
3    2012-01-11

SQL:

SELECT date, uid
FROM users
GROUP by date,`uid` 

结果:

uid   date
1    2012-01-10
2    2012-01-10
3    2012-01-09
3    2012-01-11

因为我需要按日期分页这些记录,如果我使用sql:

SELECT date, uid
FROM users
GROUP by date,`uid`
LIMIT 0,2

然后我就得到这样的记录:

uid   date
1    2012-01-10
2    2012-01-10

。如何按日期分页记录。 页面大小为2时我想要的结果:

uid   date
1    2012-01-10
2    2012-01-10
3    2012-01-09

1 个答案:

答案 0 :(得分:1)

SELECT date, uid
FROM users
WHERE date >= 'your date'
  AND date < 'your date' LIMIT 0, 3