5

我正在向用户显示当前周事件..从当前日期获取数据到当前日期值的 +7 天..它通过使用这些查询来实现

SELECT * from events WHERE ( start_date BETWEEN '2013-01-01' AND '2013-01-07' ) order by start_date ASC

我有问题,如果,

start_date 2013-01-01 have 10 records
start_date 2013-01-02 have 1 records
start_date 2013-01-03 have 12 records
....
...

然后从数据库中获取数据需要更多时间。所以,我每天只需要显示 3 个数据表

我的表,

id      start_date      end_date        event_title

1       2013-01-01      2013-01-03      event_1
2       2013-01-01      2013-01-01      event_2
3       2013-01-01      2013-01-01      event_3
4       2013-01-01      2013-01-01      event_4
-       2013-01-01      2013-01-01      event_4
-       2013-01-01      2013-01-01      event_4

5       2013-01-02      2013-01-02      event_5

6       2013-01-03      2013-01-03      event_6
7       2013-01-03      2013-01-03      event_7
8       2013-01-03      2013-01-03      event_8
9       2013-01-03      2013-01-03      event_9

10      2013-01-04      2013-01-04      event_10

预期输出为 ,

id      start_date      end_date        event_title

1       2013-01-01      2013-01-03      event_1
2       2013-01-01      2013-01-01      event_2
3       2013-01-01      2013-01-01      event_3

4       2013-01-02      2013-01-02      event_5

5       2013-01-03      2013-01-03      event_6
6       2013-01-03      2013-01-03      event_7
7       2013-01-03      2013-01-03      event_8

8       2013-01-04      2013-01-04      event_10

任何人都可以帮助解决这些... 可能在单个查询中

4

2 回答 2

4

此查询应该有效

SET @level = 0;
SET @group = '';
SELECT 
    *
FROM (
    SELECT 
        id,
        start_date,
        end_date,
        event_title,
        @level := IF(@group = start_date, @level+1, 1) AS LEVEL, 
        @group := start_date AS StartDate
    FROM test
    /*WHERE start_date BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 7 DAY)*/
    ORDER BY start_date 
) rs
WHERE LEVEL < 4 

这将在每个日期获取 3 条记录。如果你想申请 where 条件根据你的需要取消注释

演示

输出

id  start_date  end_date    event_title level StartDate 
1   2013-01-01  2013-01-03  event_1     1   2013-01-01 
2   2013-01-01  2013-01-01  event_2     2   2013-01-01 
3   2013-01-01  2013-01-01  event_3     3   2013-01-01 
7   2013-01-02  2013-01-02  event_5     1   2013-01-02 
8   2013-01-03  2013-01-03  event_6     1   2013-01-03 
9   2013-01-03  2013-01-03  event_7     2   2013-01-03 
10  2013-01-03  2013-01-03  event_8     3   2013-01-03 
12  2013-01-04  2013-01-04  event_10    1   2013-01-04 
于 2013-01-10T07:00:12.263 回答
2

试试这个:

SELECT id, start_date, end_date, event_title 
FROM (SELECT id, start_date, end_date, event_title, 
             IF(@lastDate=(@lastDate:=start_date), @auto:=@auto+1, @auto:=0) autoCol 
      FROM events e, (SELECT @lastDate:='', @auto:=0) A 
      WHERE start_date BETWEEN '2013-01-01' AND '2013-01-07' 
      ORDER BY start_date ASC) A 
WHERE autoCol < 3;

检查此链接SQL FIDDLE DEMO

输出

id  start_date  end_date    event_title 
1   2013-01-01  2013-01-03  event_1     
2   2013-01-01  2013-01-01  event_2     
3   2013-01-01  2013-01-01  event_3     
7   2013-01-02  2013-01-02  event_5     
8   2013-01-03  2013-01-03  event_6     
9   2013-01-03  2013-01-03  event_7     
10  2013-01-03  2013-01-03  event_8     
12  2013-01-04  2013-01-04  event_10    
于 2013-01-10T06:29:11.923 回答