Your query is fine. It should work as expected:
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)
Are you sure that your timein
and timeout
fields are of type datetime
or timestamp
?
UPDATE: Further to @Psytronic's suggestion the comments below, your example would even work if your fields were of varchar
type:
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)
However your fields should not be varchar
, as the above would be doing a string comparison instead of a time comparison.