views:

30

answers:

2

Can anyone help me on how did MySQL can't read a rows of I've try to execute this query.

SELECT * FROM attendance where '2010-07-13 00:06:00' BETWEEN timein AND timeout;

This is the date in the table:

+-----------------------+-----------------------+
|  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  |
+-----------------------+-----------------------+

as we can see on the table, row 2 is expected to meet the date validation but when I execute the query, it returns no result. Can anyone help if there was an alternative queries to do it.

+3  A: 

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.

Daniel Vassallo
I have already a data in a table and want to catch a row 2 as a result but it returns 0 result..
Zen
@Zen: Yes, this is just a test case. It shows that your query works. I created a new table, inserted the same data as in your example, and copy/pasted your query. It returned the expected result (row 2)
Daniel Vassallo
@Zen: Are you sure that your `timein` and `timeout` fields are of type `datetime` or `timestamp`?
Daniel Vassallo
I know it would have some massive overhead, only temporary though, but could you cast the timein and timeout as dates, to see if that makes a difference? If it does then chances are the two columns aren't datetime fields.
Psytronic
@Psytronic: Interesting. It would work as well, but I guess it is not reliable, as it should be doing string comparisons... `EDIT`: Or did you intend to test something else, now that I re-read your comment :) ?
Daniel Vassallo
yes, I've checked the types of does fields and it's set as datetime type..
Zen
Ohh.. Annoying Database execution, It's now fetched a result.. I don't know how does it comes..
Zen
very interesting that it would even work with varchar cookies for the test and time invested... but even if it does work as varchar the OP should use it with either one of the time field types... timestamp, datetime, etc...
Prix
@Prix: Yes of course. It's working now for the OP, so it was probably something else :)
Daniel Vassallo
A: 

You'll want to do something like:

SELECT * FROM attendance where timein >= '2010-07-13 00:06:00' and timeout <= '2010-07-13 17:00:00';

EDIT: I defer to the experts here, but this is how I would do it.

Randolph Potter
I try this one but still not working.. :(
Zen