How to check a dates of a date range lies in between two dates in mysql query

元气小坏坏 提交于 2020-02-05 03:25:06

问题


I have a date range like

date from=2011-10-14 & date to=2011-10-20

if I have another date ranges

like

- 2011-10-11 - 2011-10-15
- 2011-10-11 - 2011-10-21
- 2011-10-15 - 2011-10-21
- 2011-10-15 - 2011-10-19
- 2011-10-21 - 2011-10-26

I want sql query which shows only the date range whose dates lies in between above (2011-10-14 & date to=2011-10-20) date range.

Here only 2011-10-21 - 2011-10-26 does not lies in date from=2011-10-14 & date to=2011-10-20

Result must show

- 2011-10-11 - 2011-10-15
- 2011-10-11 - 2011-10-21
- 2011-10-15 - 2011-10-21
- 2011-10-15 - 2011-10-19

these dates

Hope you understand what I am asking.


回答1:


date_from < '2011-10-20' AND date_to > '2011-10-14'



回答2:


SELECT * FROM datetable where mydate BETWEEN '2011-10-14' AND '2011-10 20';



回答3:


WHERE date_from >= '2011-10-14' AND date_to <= '2011-10-20'?

But this will only work if you have your column's data type set to DATE or DATETIME




回答4:


You can go and search on the Internet :-)

The first links you will come across is this: http://www.c-sharpcorner.com/Blogs/692/

Hope the above link helps in resolving your query.

Or, you could give something like this:

SELECT * FROM myTBL WHERE myReqDate NOT BETWEEN '2011-10-14' AND '2011-10-20'



来源:https://stackoverflow.com/questions/7304504/how-to-check-a-dates-of-a-date-range-lies-in-between-two-dates-in-mysql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!