简体   繁体   English

如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行

[英]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的评论。

SQL Fiddle SQL小提琴

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.

相关问题 MYSQL - 如何在同一个表上的两个不同行上减去两列? - MYSQL - How may i subtract two columns on two different rows, on the same table? 同一张表的两个不同列的MySql Distinct值 - MySql Distinct values of two different columns of the same table 如何从mysql中具有不同条件的表中选择两个不同的值? - How to select two different values from a table with different conditions in mysql? 查找表中两个不同列之间的值 - Find Values between two different columns in a table 在MySQL的同一列中选择两个不同的值 - Select Two different values in the same column in MySQL 如何找到 mysql 中同一列不同行中的两个值之间的差异 - How to find the difference between two values in same column different rows in mysql mysql:如何使用另一个表的两个不同值从一个表中选择两个不同的列值 - mysql :How to select two different column values from a single table using two different values of another table 如何在同一张表的两个不同列中进行聚合并加入mysql? - How to perform aggregation in two different columns of same table and join in mysql? 如何选择同一表中具有特定列中给定值的两个不同行 - How to select two different rows in same table having given values in a specific column MySQL:分别合并两个相关行的两个不同列的值 - MySQL: Combine the values of two different columns of two associated rows respectively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM