获取特定时间范围内的数据

时间:2018-10-02 02:38:43

标签: mysql sql

我正在尝试选择从2018-09-24到2018-09-26的每天晚上7点至早上7点之间的记录。
示例:

  

2018-09-24 _____晚上7点至7点____ 2018-09-25
  2018-09-25 _____晚上7点至上午7点____ 2018-09-26
  2018-09-26 _____晚上7点至上午7点____ 2018-09-27

通过使用此代码A:我从7pm(2018-09-24)到7am(2018-09-26)都获得记录。

If Trim(cbShift.Text) = "Morning" Then
startdt = (S_date.Text) & " 19:00:00.317"
enddt = (E_date.Text) & " 07:00:00.160" End If
SELECT FROM tb_test
WHERE  (DateCreate   BETWEEN convert(datetime,@startdt) AND convert(datetime,@enddt))

并使用此代码B:

WHERE  DateCreate > convert(datetime,'2018-09-24') AND DateCreate <=
DATEADD(day,1,convert(datetime,'2018-09-26'))
AND (DATEPART(hh,DateCreate) >= 19 and DATEPART(hh,DateCreate) <= 24  )

or DateCreate > convert(datetime,'2018-09-24') AND DateCreate <=
DATEADD(day,1,convert(datetime,'2018-09-26'))
AND (DATEPART(hh,DateCreate) >= 0 and DATEPART(hh,DateCreate) <= 6  )

我知道

  

2018-09-24(00-> 7和19-> 24),2018-09-25(00-> 7和19-> 24)和2018-09-26(00-> 7和19) -> 24)

有没有办法像上面的示例那样获取数据。 谢谢您的帮助

3 个答案:

答案 0 :(得分:2)

我认为问题在于您的WHERE子句中的评估顺序。我始终在SQL的OR运算符周围使用括号。以下内容适用于MySQL 5.6。

SELECT * 
FROM tb_test
WHERE  datecreate between '2018-09-24 19:00:00' and '2018-09-26 07:00:00'
AND (hour(DateCreate) >= 19 or hour(DateCreate) < 7);

作为参考,我使用了以下测试模式:

CREATE table tb_test
(datecreate datetime);

CREATE OR REPLACE VIEW generator_16
AS SELECT 0 n UNION ALL SELECT 1  UNION ALL SELECT 2  UNION ALL 
 SELECT 3   UNION ALL SELECT 4  UNION ALL SELECT 5  UNION ALL
 SELECT 6   UNION ALL SELECT 7  UNION ALL SELECT 8  UNION ALL
 SELECT 9   UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL
 SELECT 12  UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL 
 SELECT 15;

CREATE OR REPLACE VIEW generator_256
AS SELECT ( ( hi.n << 4 ) | lo.n ) AS n
 FROM generator_16 lo, generator_16 hi;

INSERT INTO tb_test
SELECT date_add("2018-09-24",interval n hour)
FROM generator_256;

答案 1 :(得分:1)

解决问题的另一种方法是说,您希望main的-12小时位于感兴趣的日子的上午7点至晚上7点之间。因此,请尝试以下操作(请注意,我们不会为DateCreatestartdt添加时间):

enddt

答案 2 :(得分:0)

让我们创建一些测试数据:

n = <the number of bombs>
for i in xrange(n):
    x = i % width  # x will iterate over 0 to width
    y = r.randint(0, height - 1)
    # insert bomb

现在,我们要从mysql> create table t (id int auto_increment, stamp datetime, primary key (id)); Query OK, 0 rows affected (0.46 sec) mysql> insert into t (stamp) values -> ('2018-09-24 18:59:59'), ('2018-09-24 19:00:00'), ('2018-09-24 19:00:01'), -> ('2018-09-25 06:59:59'), ('2018-09-25 07:00:00'), ('2018-09-25 07:00:01'), -> ('2018-09-25 18:59:59'), ('2018-09-25 19:00:00'), ('2018-09-25 19:00:01'), -> ('2018-09-26 06:59:59'), ('2018-09-26 07:00:00'), ('2018-09-26 07:00:01'), -> ('2018-09-26 18:59:59'), ('2018-09-26 19:00:00'), ('2018-09-26 19:00:01'), -> ('2018-09-27 06:59:59'), ('2018-09-27 07:00:00'), ('2018-09-27 07:00:01'); Query OK, 18 rows affected (0.00 sec) Records: 18 Duplicates: 0 Warnings: 0 mysql> select * from t; +----+---------------------+ | id | stamp | +----+---------------------+ | 1 | 2018-09-24 18:59:59 | | 2 | 2018-09-24 19:00:00 | | 3 | 2018-09-24 19:00:01 | | 4 | 2018-09-25 06:59:59 | | 5 | 2018-09-25 07:00:00 | | 6 | 2018-09-25 07:00:01 | | 7 | 2018-09-25 18:59:59 | | 8 | 2018-09-25 19:00:00 | | 9 | 2018-09-25 19:00:01 | | 10 | 2018-09-26 06:59:59 | | 11 | 2018-09-26 07:00:00 | | 12 | 2018-09-26 07:00:01 | | 13 | 2018-09-26 18:59:59 | | 14 | 2018-09-26 19:00:00 | | 15 | 2018-09-26 19:00:01 | | 16 | 2018-09-27 06:59:59 | | 17 | 2018-09-27 07:00:00 | | 18 | 2018-09-27 07:00:01 | +----+---------------------+ 18 rows in set (0.00 sec) 2018-09-24 19:00:00中进行选择:

2018-09-25 07:00:00

如果我们想在这些窗口中包含3天的数据,可以使用mysql> select * from t where stamp between (@start := '2018-09-24 19:00:00') and date_add(@start, interval 12 hour); +----+---------------------+ | id | stamp | +----+---------------------+ | 2 | 2018-09-24 19:00:00 | | 3 | 2018-09-24 19:00:01 | | 4 | 2018-09-25 06:59:59 | | 5 | 2018-09-25 07:00:00 | +----+---------------------+ 4 rows in set (0.01 sec)

UNION ALL

就是这样。

编辑后添加:在看到Nick使用mysql> select * from t where stamp between (@start := '2018-09-24 19:00:00') and date_add(@start, interval 12 hour) -> union all select * from t where stamp between (@start := '2018-09-25 19:00:00') and date_add(@start, interval 12 hour) -> union all select * from t where stamp between (@start := '2018-09-26 19:00:00') and date_add(@start, interval 12 hour); +----+---------------------+ | id | stamp | +----+---------------------+ | 2 | 2018-09-24 19:00:00 | | 3 | 2018-09-24 19:00:01 | | 4 | 2018-09-25 06:59:59 | | 5 | 2018-09-25 07:00:00 | | 8 | 2018-09-25 19:00:00 | | 9 | 2018-09-25 19:00:01 | | 10 | 2018-09-26 06:59:59 | | 11 | 2018-09-26 07:00:00 | | 14 | 2018-09-26 19:00:00 | | 15 | 2018-09-26 19:00:01 | | 16 | 2018-09-27 06:59:59 | | 17 | 2018-09-27 07:00:00 | +----+---------------------+ 12 rows in set (0.00 sec) 的答案之后,这实际上是一个更干净的解决方案:

TIME()