任何人都可以帮我解决MySQL如何读取我试图执行此查询的行。
SELECT * FROM attendance where '2010-07-13 00:06:00' BETWEEN timein AND timeout;
这是表格中的日期:
+-----------------------+-----------------------+
| timein | timeout |
------------------------+------------------------
| 2010-07-13 23:44:11 | 2010-07-14 08:01:14 |
| 2010-07-12 23:40:56 | 2010-07-13 08:00:52 |
| 2010-07-10 05:49:32 | 2010-07-10 14:00:45 |
+-----------------------+-----------------------+
正如我们在表中看到的那样,第2行预计会满足日期验证,但是当我执行查询时,它不会返回任何结果。如果有替代查询可以帮助任何人。
答案 0 :(得分:5)
您的查询没问题。它应该按预期工作:
CREATE TABLE attendance (id int, timein datetime, timeout datetime);
INSERT INTO attendance VALUES (1, '2010-07-13 23:44:11', '2010-07-14 08:01:14');
INSERT INTO attendance VALUES (2, '2010-07-12 23:40:56', '2010-07-13 08:00:52');
INSERT INTO attendance VALUES (3, '2010-07-10 05:49:32',' 2010-07-10 14:00:45');
SELECT * FROM attendance where '2010-07-13 00:06:00' BETWEEN timein AND timeout;
+------+---------------------+---------------------+
| id | timein | timeout |
+------+---------------------+---------------------+
| 2 | 2010-07-12 23:40:56 | 2010-07-13 08:00:52 |
+------+---------------------+---------------------+
1 row in set (0.01 sec)
您确定timein
和timeout
字段的类型为datetime
或timestamp
吗?
更新:在@Psytronic's建议下面的评论之后,如果您的字段属于varchar
类型,您的示例甚至会有用:
CREATE TABLE attendance (id int, timein varchar(100), timeout varchar(100));
INSERT INTO attendance VALUES (1, '2010-07-13 23:44:11', '2010-07-14 08:01:14');
INSERT INTO attendance VALUES (2, '2010-07-12 23:40:56', '2010-07-13 08:00:52');
INSERT INTO attendance VALUES (3, '2010-07-10 05:49:32',' 2010-07-10 14:00:45');
SELECT * FROM attendance where '2010-07-13 00:06:00' BETWEEN timein AND timeout;
+------+---------------------+---------------------+
| id | timein | timeout |
+------+---------------------+---------------------+
| 2 | 2010-07-12 23:40:56 | 2010-07-13 08:00:52 |
+------+---------------------+---------------------+
1 row in set (0.00 sec)
但是,您的字段不应为varchar
,因为上面的内容会进行字符串比较,而不是时间比较。
答案 1 :(得分:1)
你会想做类似的事情:
SELECT * FROM attendance where timein >= '2010-07-13 00:06:00' and timeout <= '2010-07-13 17:00:00';
编辑:我在这里尊重专家,但这就是我要做的。