Selecting rows from db based on date

I have a list of rows 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 need to select rows that are between the date 2012-01-01 - 2012-01-05. How can I do this, please help.

+4
source share
5 answers
SELECT Off_Id,uid,Leave_from,leave_to,leave_code FROM YourTableName WHERE Leave_From>="2012-01-01" and Leave_to <="2012-01-05" 
+1
source
 SELECT * FROM tbl WHERE leave_from >= '2012-01-01' AND leave_to <= '2012-01-05' 
+3
source
 select * from table where leave_from between '2012-01-01' and '2012-01-05' 
+2
source

If I understand your question correctly:

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

point 1: you can use> <<=> = with date dates.

the above can be achieved using

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

Source: https://habr.com/ru/post/1389698/


All Articles