Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 对组中的日期数据进行分页_Mysql_Sql_Group By_Pagination_Sql Order By - Fatal编程技术网

Mysql 对组中的日期数据进行分页

Mysql 对组中的日期数据进行分页,mysql,sql,group-by,pagination,sql-order-by,Mysql,Sql,Group By,Pagination,Sql Order By,我有一张这样的桌子: create (DATETIME) | Message (TEXT) ========================================= 2013-08-05 9:00:00 "Hi there!" 2013-08-05 10:00:00 "Hi again!" 2013-08-07 14:00:00 "Hi from the future!" 我希望能够在DATEcreate上获取所有消息和分组,以便按2013-08-05、2013-08-07等对

我有一张这样的桌子:

create (DATETIME) |   Message (TEXT)
=========================================
2013-08-05 9:00:00  "Hi there!"
2013-08-05 10:00:00 "Hi again!"
2013-08-07 14:00:00 "Hi from the future!"
我希望能够在DATEcreate上获取所有消息和分组,以便按2013-08-05、2013-08-07等对消息进行分组。这可以通过以下方式轻松完成:

问题是我希望能够分组分页。例如,如果我有10个不同的日期,我想显示所有消息,每页5个日期组,并分页

我已经看到了很多限制组内元素数量的解决方案,但还没有看到任何限制组数量的解决方案

我基本上希望得到如下结果:

create (DATETIME) |   Message (TEXT)
=========================================
2013-08-05 9:00:00  "Hi there!"
2013-08-05 10:00:00 "Hi again!"
2013-08-07 14:00:00 "Hi from the future!"
如果限制为仅显示1组:

2013-08-05 9:00:00  "Hi there!"
2013-08-05 10:00:00 "Hi again!"
如果限制显示2组:

2013-08-05 9:00:00  "Hi there!"
2013-08-05 10:00:00 "Hi again!"
2013-08-07 14:00:00 "Hi from the future!"
如果限制显示跳过第一个组的1个组:

2013-08-07 14:00:00 "Hi from the future!"

如何在SQL中实现这一点?

我认为您需要计算组id并手动执行限制:

SELECT m.*
FROM messages m join
     (select date(create) as createdate, count(*) as cnt, @rn := @rn + 1 as groupnum
      from messages m cross join (select @rn := 0) const
      group by date(create)
     ) md
     on date(m.create) = createdate
WHERE groupnum between 1 and 5;
你可以这样做

仅限显示1组:

2013-08-05 9:00:00  "Hi there!"
2013-08-05 10:00:00 "Hi again!"
输出:

+---------------------+-----------+ | created | message | +---------------------+-----------+ | 2013-08-05 9:00:00 | Hi there! | | 2013-08-05 10:00:00 | Hi again! | +---------------------+-----------+ +---------------------+---------------------+ | created | message | +---------------------+---------------------+ | 2013-08-07 14:00:00 | Hi from the future! | +---------------------+---------------------+ 输出:

+---------------------+-----------+ | created | message | +---------------------+-----------+ | 2013-08-05 9:00:00 | Hi there! | | 2013-08-05 10:00:00 | Hi again! | +---------------------+-----------+ +---------------------+---------------------+ | created | message | +---------------------+---------------------+ | 2013-08-07 14:00:00 | Hi from the future! | +---------------------+---------------------+ 注意:确保在创建的列上有索引


下面是演示

示例数据更清晰:

查询1:从0开始的1组计数

SELECT m1.* FROM messages m1 JOIN (
  SELECT DISTINCT date(created) OnlyDate FROM messages
  ORDER BY OnlyDate
  LIMIT 0,1
) m2 ON date(m1.created) = OnlyDate;

|             CREATED |   MESSAGE |
|---------------------|-----------|
|  2013-08-05 9:00:00 | Hi there! |
| 2013-08-05 10:00:00 | Hi again! |
查询2:1组从1开始计数

SELECT m1.* FROM messages m1 JOIN (
  SELECT DISTINCT date(created) OnlyDate FROM messages
  ORDER BY OnlyDate
  LIMIT 1,1
) m2 ON date(m1.created) = OnlyDate

|             CREATED |             MESSAGE |
|---------------------|---------------------|
| 2013-08-07 14:00:00 | Hi from the future! |
您只需使用limit子句处理组,第一个值是起始组,第二个值是从它开始的偏移量。查询也应该相当有效,并且也不难阅读


Fiddle.

我希望能够获得所有消息和组=>这是什么意思?您能提供您的预期输出吗?@MostyMostacho问题更新: