Home Database Mysql Tutorial MySQL查询不含周末的五天前的日期_MySQL

MySQL查询不含周末的五天前的日期_MySQL

Jun 01, 2016 pm 02:02 PM
us date Inquire this

我需要查询从现在算起五天前的日期。按照商业习惯,这五天应该不包含星期六和星期天。

专家回答:

对于许多跟商业日期有关的情况,最好的解决方案是使用日历表格。例如,使用办公时间(2001年5月22日)查询两个日期之间的时间差。

我们知道在这个例子中,假日不会计算在内。很难明白为什么假日不被计算在内,但是周末却会被计算在内。但是既然假日不被计算在内,我们就可以应用一个公式。要开发这个公式,让我们首先探讨一下所有的可能性。

如果今天是星期一,那么,往回数,我们跳过星期天和星期六,星期五是一天前,星期四是两天前,如此类推,那么“五天前”就是上个星期一。这里的“五天前”就意味着把周末排除出去,得出我们想要的日期,并不是真正字面意义上的五天前。我们可以从备忘录上看到差别。

如果今天是星期二,那么,往回数,星期一是一天前,跳过星期天和星期六,星期五是两天前,星期四是三天前,如此类推,那么“五天前”就是上个星期二。

这个模式——“五天前”就是上一个星期的同一天(星期X)——可重复到星期三、星期四和星期五。

到了星期六,这个模式就不可用了。在星期六,五天前是星期一。在星期天,因为我们不计算星期六,五天前也是星期一。

把我们的讨论发现总结成如下数据:

MySQL查询不含周末的五天前的日期

要获得“五天前”的日期,从今天减去的总天数(包括周末)显示在右边列中。

我们给一个星期里的每一天排上序号,星期天(Sunday)=1,星期一(Monday)=2,如此类推,到星期六(Saturday)=7。把这些日子的序号排列在需要减去的天数前,我们会得到如下数据:

if today is

weekday subtract

Monday

2 7

Tuesday

3 7
Wednesday 4 7

Thursday

5 7

Friday

6 7

Saturday

7 5

Sunday

1 6

 

公式如下:

subtract = 7 - 2*(weekday/7) + (weekday-2)/7

记住,这是要获得“五天前”的日期而需要从今天减去的天数。在这个公式里,除法是整数除法(即下舍入)。请不要问我这个公式是怎么发现的,反正是试差法的成果。

我们要怎么把这个公式应用到SQL里呢?下面是一个运用MySQL句法的例子:

<font face="新宋体"></font>

以下为引用的内容:

selectdistinct 
   cust.fname
  ,cust.lname
  ,cust.phone
 fromordersaso
inner
 joincustomersascust
  oncust.id=orders.cust_id
whereo.date_ordered=
   date_sub(current_date
       ,interval
   7-2*floor(dayofweek(current_date)/7)
    +floor((dayofweek(current_date)-2)/7)
          day)
 ando.date_shippedisnull

<font face="新宋体"></font>

 

这个查询可以获得“五天前”下订单而货还没有运到的客户的联系方式。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo How to search previous Weibo by date on Weibo_How to search previous Weibo by date on Weibo Mar 30, 2024 pm 07:26 PM

1. First open the mobile web browser, search for the Weibo web version, and click the avatar button in the upper left corner after entering. 2. Then click Settings in the upper right corner. 3. Click the version switching option in settings. 4. Then select the color version option in the version switch. 5. Click Search to enter the search page. 6. After entering the keywords, click Find People. 7. When the search completion interface appears, click Filter. 8. Finally, enter the specific date in the release time column and click Filter.

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

How to remove the date that appears automatically when printing from PPT handouts How to remove the date that appears automatically when printing from PPT handouts Mar 26, 2024 pm 08:16 PM

1. Let me first talk about the method I used at the beginning, maybe everyone is using it too. First, open [View]——]Remarks Template[. 2. A place where you can actually see the date after opening it. 3. Select it first and delete it. 4. After deleting, click [Close Master View]. 5. Open the print preview again and find that the date is still there. 6. In fact, this date was not deleted here. It should be in the [Handout Master]. Look at the picture below. 7. Delete the date after you find it. 8. Now when you open the preview and take a look, the date is no longer there. Note: In fact, this method is also very easy to remember, because the printed handouts are handouts, so you should look for the [Handout Master].

How to change the date into a pound sign in Excel How to change the date into a pound sign in Excel Mar 20, 2024 am 11:46 AM

Excel software has very powerful data processing functions. We often use excel software to process various data. Sometimes when we enter a date in an excel cell, the date in excel changes to a pound sign. How can we display the data normally? Let’s take a look at the solution below. 1. First, we put the mouse on the column width line between columns AB, double-click and adjust the column width, as shown in the figure below. 2. After the column is widened, we find that numbers are displayed in the cells instead of dates. This is definitely incorrect. Then we should check the format of the cells, as shown in the figure below. 3. Click the &quot;Number&quot; option in the &quot;Home&quot; tab, and click &quot;Other Number Format&quot; in the drop-down menu, as shown in the figure below.

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages ​​respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

How to check the activation date on Apple mobile phone How to check the activation date on Apple mobile phone Mar 08, 2024 pm 04:07 PM

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User

How to determine whether a date is the previous day in Go language? How to determine whether a date is the previous day in Go language? Mar 24, 2024 am 10:09 AM

Question: How to determine whether the date is the previous day in Go language? In daily development, we often encounter situations where we need to determine whether the date is the previous day. In the Go language, we can implement this function through time calculation. The following will be combined with specific code examples to demonstrate how to determine whether the date is the previous day in Go language. First, we need to import the time package in the Go language. The code is as follows: import(&quot;time&quot;) Then, we define a function IsYest

See all articles