简体   繁体   English

从MYSQL表中选择行,其中日期在包含周末的两列之间

[英]Select rows from MYSQL table where dates between two columns containing weekend

I want to select rows from a MYSQL table where dates between two columns start_date, end_date containing weekends 我想从MYSQL表中选择行,其中两列之间的日期start_date,end_date包含周末

I tried found this but it is not helpful 我试图找到这个,但没有帮助

My table columns are Name start_date end_date amount 我的表列是名称开始日期结束日期数量

I want to print name and amount if days between start_date and end_date containing Saturday and Sunday. 如果起始日期和结束日期之间的日期包含星期六和星期日,我想打印名称和金额。

Use DAYNAME function. 使用DAYNAME函数。 It gives Dayname of a particular date. 它给出特定日期的Dayname。

Try this, 尝试这个,

select * from yourtable where 
    (DAYNAME(start_date) = 'Saturday' or DAYNAME(start_date) = 'Sunday') and
    (DAYNAME(end_date) = 'Saturday' or DAYNAME(end_date) = 'Sunday'); 

Hope it will help. 希望它会有所帮助。

PS I din't run this query. PS我没有运行此查询。

From what I understand, you want to take two columns as start and end dates and count the number of weekend days between them. 据我了解,您想将两列用作开始和结束日期,并计算它们之间的周末天数。 Is that correct? 那是对的吗?

This answer How to get list of dates between two dates in mysql select query has some SQL for getting a list of all dates in a range. 这个答案如何在mysql select查询中获取两个日期之间的日期列表有一些SQL用于获取范围内所有日期的列表。

To build on that you could use that query as an inner join, selecting only the dates you need based on your start and end columns. 要以此为基础,您可以将该查询用作内部联接,根据开始和结束列仅选择所需的日期。 Then count Sundays and Saturdays using DAYNAME. 然后使用DAYNAME计算周日和周六。

Here's an example that gets close to what you should use. 这是一个接近您应使用的示例。 This is not exactly what you should use! 这不完全是您应该使用的! This is only built for hard coding the dates (and very close to passing them in as parameters). 这仅用于对日期进行硬编码(并且非常接近于将其作为参数传递)。 You should adapt it by joining your table to it! 您应该通过将表加入其中来进行调整! . I didn't write it that way because: I'm lazy and didn't want to make a table for testing and demonstrating it, and (more importantly) I wanted to help you and not just hand over a solution. 我之所以没有这样写,是因为:我很懒惰,不想创建一个表格来测试和演示它,(更重要的是)我想为您提供帮助,而不仅仅是提供解决方案。

select count(one_date), dayname(one_date) week_day from

(select * from 
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) one_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v ) as all_days

where one_date between '2016-10-01' and '2016-11-15' -- <---- DON'T HARDCODE THESE, JOIN YOUR TABLE TO THE all_days AND FILTER THE DAYS YOU WANT THAT WAY
group by week_day
having week_day in ('Sunday', 'Saturday');

This is a lengthy query, may slowdown the performance. 这是一个冗长的查询,可能会降低性能。 But this can give you desired output 但这可以给你想要的输出

SELECT x.id,x.name,x.amount FROM 
    (SELECT name,amount,DATEDIFF(end_date,start_date)+1 AS totDays,
       start_date AS day1,ADDDATE(start_date,1) AS day2,
       ADDDATE(start_date,2) AS day3,ADDDATE(start_date,3) AS day4,
       ADDDATE(start_date,4) AS day5,ADDDATE(start_date,5) AS day6,
       end_date FROM your_table)X 
     WHERE x.totDays>5 OR (WEEKDAY(x.day1)>4 AND x.day1<=x.end_date) OR 
     (WEEKDAY(x.day2)>4 AND x.day2<=x.end_date) OR 
     (WEEKDAY(x.day3)>4 AND x.day3<=x.end_date) OR 
     (WEEKDAY(x.day4)>4 AND x.day4<=x.end_date) OR 
     (WEEKDAY(x.day5)>4 AND x.day5<=x.end_date) OR 
     (WEEKDAY(x.day6)>4 AND x.day6<=x.end_date);

Or try this, 或者尝试一下

SELECT name,amount FROM your_table WHERE
    (DATEDIFF(end_date,start_date)+1) >5 OR 
    (WEEKDAY(start_date)>4 AND start_date<=end_date) OR 
    (WEEKDAY(ADDDATE(start_date,1))>4 AND ADDDATE(start_date,1)<=end_date) OR 
    (WEEKDAY(ADDDATE(start_date,2))>4 AND ADDDATE(start_date,2)<=end_date) OR 
    (WEEKDAY(ADDDATE(start_date,3))>4 AND ADDDATE(start_date,3)<=end_date) OR 
    (WEEKDAY(ADDDATE(start_date,4))>4 AND ADDDATE(start_date,4)<=end_date) OR 
    (WEEKDAY(ADDDATE(start_date,5))>4 AND ADDDATE(start_date,5)<=end_date);

Notes: 笔记:

If DATEDIFF between start_date and end_date is greater than 5, then there will be weekends. 如果start_dateend_date之间的DATEDIFF大于5,则将有周末。

ADDDATE with start_date up to 5 and compare that with end_date and if WEEKDAY of the output is greater than 4, then there will be weekends. ADDDATEstart_date长达5和比较,与end_date如果WEEKDAY输出大于4,那么就会有周末。

If you want to select rows where between start and end date there is a whole weekend (Saturday and Sunday together) you may use this query: 如果要选择在开始日期和结束日期之间有一个整个周末(星期六和星期日在一起)的行,则可以使用以下查询:

select name, amount FROM your_table WHERE 
   datediff(end_date,start_date) > 6 
   or 
   (datediff(end_date,start_date)+weekday(start_date)>=6 
    and weekday(start_date)<>6);

Assuming start_date and end_date inclusive datediff(end_date,start_date) for the whole week is equal 6, so if there is more days than one week we can be sure there is a weekend between. 假设整周的start_date和end_date包括在内的datediff(end_date,start_date)等于6,因此,如果天数多于一周,那么我们可以确定之间有一个周末。 When there is less days between we have to check if the number of days starting from the given start weekday will cover the weekend but we can be sure that it's not true if the start day is Sunday (starting from Sunday six days later is Saturday but these two weekend days are not one after another). 如果之间的天数较少,我们必须检查从给定的开始工作日开始的天数是否可以涵盖周末,但是我们可以确定,如果开始日期为星期日(从六天后的星期日开始,则为星期六,这两个周末不是一个接一个的)。

In case you want to select rows where between start and end date there is at least one weekday (Saturday or Sunday) you can use this query 如果您要选择开始日期和结束日期之间至少有一个工作日(星期六或星期日)的行,则可以使用此查询

select name, amount FROM your_table WHERE 
   datediff(end_date,start_date)+weekday(start_date)>=5;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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