我想选择所有数据(行),这些数据(行)位于每天的06:45和07:15之间,为11月10月。
select *
from
table1
where
NOT(
(hour(convert_tz(starts_at,'gmt','us/eastern')+minute(convert_tz(ends_at,'gmt','us/eastern'))) < 06.45 AND
(hour(convert_tz(ends_at,'gmt','us/eastern'))+minute(convert_tz(ends_at,'gmt','us/eastern'))) > 07.15
) AND
convert_tz(starts_at,'gmt','us/eastern') > '2015-10-01 00:00:00.000' AND
convert_tz(ends_at,'gmt','us/eastern') < '2015-12-01 00:00:00.000'
order by starts_at, ends_at
通过此查询,我获取了整天(从00到23小时)的数据,而不仅仅是我想要的时间段。
查询,
select
convert_tz(starts_at,'gmt','us/pacific') as Starts,
convert_tz(ends_at,'gmt','us/pacific') as Ends
from
table1
where
(100 * hour(convert_tz(starts_at,'gmt','us/pacific'))+minute(convert_tz(starts_at,'gmt','us/pacific'))) >= 0645 AND
(100 * hour(convert_tz(ends_at,'gmt','us/pacific'))+minute(convert_tz(ends_at,'gmt','us/pacific'))) <= 0715 AND
convert_tz(starts_at,'gmt','us/pacific') > '2015-12-01 00:00:00.000' AND
convert_tz(ends_at,'gmt','us/pacific') < '2015-12-20 00:00:00.000'
order by starts_at, ends_at
响应(前几行的示例仅说明了获取超出范围的时间戳的问题)
+-------------------------+-------------------------+
| Starts | Ends |
+-------------------------+-------------------------+
| 2015-12-01 06:45:03.549 | 2015-12-01 06:45:35.003 |
| 2015-12-01 06:45:35.003 | 2015-12-01 06:47:00.203 |
| 2015-12-01 06:47:00.203 | 2015-12-01 06:54:11.615 |
| 2015-12-01 06:47:00.780 | 2015-12-01 06:47:40.860 |
| 2015-12-01 06:47:40.860 | 2015-12-01 06:48:52.103 |
| 2015-12-01 06:48:52.103 | 2015-12-01 06:51:33.925 |
| 2015-12-01 06:51:33.925 | 2015-12-01 06:52:34.543 |
| 2015-12-01 06:52:34.543 | 2015-12-01 07:00:00.000 |
| 2015-12-01 06:54:11.615 | 2015-12-01 06:59:59.074 |
| 2015-12-01 06:59:59.074 | 2015-12-01 07:14:59.074 |
| 2015-12-01 06:59:59.158 | 2015-12-01 07:14:59.158 |
| 2015-12-01 06:59:59.388 | 2015-12-01 07:14:59.388 |
| 2015-12-01 06:59:59.393 | 2015-12-01 07:14:59.393 |
| 2015-12-01 06:59:59.513 | 2015-12-01 07:14:59.513 |
| 2015-12-01 06:59:59.708 | 2015-12-01 07:14:59.708 |
| 2015-12-01 07:00:00.000 | 2015-12-01 07:15:00.000 |
| 2015-12-01 07:00:00.000 | 2015-12-01 07:15:00.000 |
| 2015-12-01 07:00:00.000 | 2015-12-01 07:15:00.000 |
| 2015-12-01 07:00:00.000 | 2015-12-01 07:15:00.000 |
| 2015-12-01 07:00:01.912 | 2015-12-01 07:14:59.471 |
| 2015-12-01 21:59:59.158 | 2015-12-02 00:59:59.158 |
| 2015-12-01 21:59:59.388 | 2015-12-02 06:59:59.388 |
| 2015-12-01 21:59:59.513 | 2015-12-02 06:59:59.513 |
| 2015-12-01 22:00:00.000 | 2015-12-02 01:00:00.000 |
| 2015-12-01 22:00:00.000 | 2015-12-02 01:00:00.000 |
| 2015-12-01 22:43:26.458 | 2015-12-02 05:13:23.365 |
| 2015-12-01 23:15:05.493 | 2015-12-02 05:08:38.585 |
| 2015-12-01 23:18:10.110 | 2015-12-02 05:09:29.648 |
| 2015-12-01 23:31:41.896 | 2015-12-02 00:10:16.796 |
| 2015-12-01 23:45:37.852 | 2015-12-02 00:19:22.301 |
| 2015-12-01 23:53:48.851 | 2015-12-02 00:17:20.538 |
| 2015-12-02 06:51:11.221 | 2015-12-02 06:51:16.456 |
| 2015-12-02 06:51:16.456 | 2015-12-02 07:01:19.435 |
只有2100到0045范围内的时间戳才会在整个数据集中重复出现。
答案 0 :(得分:0)
尝试类似:
select *
from
table1
where
NOT(
(time(convert_tz(starts_at,'gmt','us/eastern'))) < '06:45' AND
(time(convert_tz(ends_at,'gmt','us/eastern'))) > '07:15'
) AND
convert_tz(starts_at,'gmt','us/eastern') > '2015-10-01 00:00:00.000' AND
convert_tz(ends_at,'gmt','us/eastern') < '2015-12-01 00:00:00.000'
order by starts_at, ends_at
答案 1 :(得分:0)
Hour
+ minute
将是0(午夜)和82(23 + 59)之间的值。此外,值不能同时为< 6.45
和> 7.15
。
有几种方法可以做你想要的。以下是您的方法的要点:
(100 * hour(convert_tz(starts_at,'gmt','us/eastern')+minute(convert_tz(ends_at,'gmt','us/eastern'))) >= 0645 AND
(100 * hour(convert_tz(ends_at,'gmt','us/eastern'))+minute(convert_tz(ends_at,'gmt','us/eastern'))) <= 0715
) AND
注意没有NOT
。这会将小时分钟转换为HHMM形式的数字,可以将其与数字进行比较。
答案 2 :(得分:0)
将您的小时数提取并转换为分钟数。
SELECT
*
FROM
table1
WHERE
HOURS(starts_at) * 60 + MINUTES(starts_at) >= 405
And HOURS(ends_at) * 60 + MINUTES(ends_at) <= 435
order by
starts_at,ends_at