简体   繁体   中英

selecting the rows from db based on date

I have a list of row's in mysql tables

+--------+-----+------------+------------+------------+-----------+
| off_id | uid | leave_from | leave_to   | leave_code | reason    |
+--------+-----+------------+------------+------------+-----------+
|      1 |   1 | 2012-01-01 | 2012-01-05 | OFF        | asdsda    |
|      2 |   1 | 2012-01-15 | 2012-01-16 | OFF        | asdd      |
|      5 |   1 | 2012-02-03 | 2012-02-05 | OFF        | gfjghjhgj |
+--------+-----+------------+------------+------------+-----------+

I have to select the rows that are in between the date 2012-01-01 to 2012-01-05. How can i do this please help.

SELECT * 
FROM tbl
WHERE leave_from >= '2012-01-01'
AND leave_to <= '2012-01-05'
select * from table where leave_from between '2012-01-01' and '2012-01-05'
SELECT Off_Id,uid,Leave_from,leave_to,leave_code
FROM YourTableName
WHERE Leave_From>="2012-01-01" and Leave_to <="2012-01-05"

If I understand your question right:

SELECT * FROM `table_name` 
WHERE leave_from >= "2012-01-01" AND leave_to <= "2012-01-05"

point 1 : you could use > < <= >= with date datatypes .

the above can be achieved by

SELECT * FROM table WHERE leave_from > '2012-01-01' AND leave_from <'2012-01-05';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM