[英]How to select rows between two different columns with two different input values on same table on mysql
I have a table name as Orders. 我有一个表名作为Orders。 Orders table have two columns names are start_date,end_date 订单表有两列,名称分别为start_date,end_date
S.No start_date end_date
1 2016-04-01 2016-04-08
2 2016-04-28 2016-05-29
3 2016-05-01 2016-05-39
Now I want records between start date of 2016-04-01 and end date of 2016-04-30. 现在,我希望在2016-04-01的开始日期和2016-04-30的结束日期之间进行记录。
Please help me on this 请帮我
Assuming you want records that overlap the range: 假设您希望记录与范围重叠:
SELECT *
FROM orders
WHERE start_date <= :end_range
AND end_date >= :start_range
In your case: 在您的情况下:
SELECT *
FROM orders
WHERE start_date <= '2016-04-30'
AND end_date >= '2016-04-01'
This works as below: 其工作原理如下:
<---RANGE-->
<--> X end_date < :start_range
R <----> Returned
O <----> Returned
W <---------------> Returned
S <----> Returned
<--> X start_date > :end_range
Assuming start_date
is always <= end_date
, you may get better results with the query below, as it gives more specificity on the start_date
filter: 假设start_date
始终<= end_date
,则使用以下查询可能会获得更好的结果,因为它在start_date
过滤器上提供了更多的特异性:
select *
from Orders
where start_date between '2016-04-01' and '2016-04-30'
and end_date between '2016-04-01' and '2016-04-30'
您是否要:从起始日期介于'04 / 01/2016'和'04 / 30/2016'和结束日期介于'04 / 01/2016'和'04 / 30/2016'的订单中选择*
Is this what you're looking for? 这是您要找的东西吗?
select *
from Orders
where
(start_date >= '2016-04-01' and start_date < '2016-05-01');
Edit Update to reflect comments from OP. 编辑更新以反映来自OP的评论。
If you want only orders that ran from 4/1 - 4/30 then: 如果您只想要从4/1-4/30运行的订单,则:
SELECT *
FROM
orders
WHERE
start_date = '2016-4-1'
AND end_date = '2016-4-30'
If you want orders that either started on 4/1 OR ended on 4/30 then: 如果你想, 要么开始在4/1或4/30结束,然后命令:
SELECT *
FROM
orders
WHERE
start_date = '2016-4-1'
OR end_date = '2016-4-30'
Edit after OP comment OP评论后编辑
SELECT *
FROM
orders
WHERE
start_date between '2016-4-1' AND '2016-4-30'
OR end_date between '2016-4-1' AND '2016-4-30'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.