MySql查询提取语法之间的值

时间:2015-11-18 07:40:07

标签: mysql

这些是我在MySql数据库表中的行:

+------------+----------+-------------+
| theDate    | theHour  | theUserCode |
+------------+----------+-------------+
| 2015-11-16 | 06:30:00 | XX2111905   |
| 2015-11-16 | 21:30:37 | XX2112111   |
| 2015-11-16 | 22:21:29 | XX2112111   |
| 2015-11-16 | 17:15:18 | XX2142122   |
| 2015-11-16 | 04:22:13 | XX2146905   |
| 2015-11-16 | 15:15:00 | XX2146905   |
| 2015-11-16 | 21:26:00 | XX2148516   |
+------------+----------+-------------+
7 rows in set

我需要从此表中提取theHour15:00:00之间03:00:00行的行。

我尝试过这个Sql查询,但在输出中我有Empty set

SELECT
    theDate,
    theHour,
    theUserCode
FROM
    `tblUserRegistered`
WHERE
    theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY)
AND '2015-11-16'
AND theHour BETWEEN '15:00:00'
AND '03:00:00'
ORDER BY
theUserCode,
theDate,
theHour ASC;

请帮帮我,非常感谢你。

1 个答案:

答案 0 :(得分:0)

我在下面的查询中使用了HOUR()函数来按小时处理检查。此查询将包括稍后的记录,而不是下午3点(15小时)或早于而不是凌晨3点(03小时)。

SELECT theDate, theHour, theUserCode
FROM tblUserRegistered
WHERE theDate BETWEEN DATE_SUB('2015-11-16', INTERVAL 1 DAY) AND '2015-11-16'
    AND (HOUR(theHour) >= 15 OR HOUR(theHour) <= 3)
ORDER BY theUserCode, theDate, theHour ASC