MySQL 中的 YEARWEEK()函数
MySQL 中的YEARWEEK ()函数用于查找给定日期的年份和星期。如果日期为 NULL,则 YEARWEEK()函数将返回 NULL。否则,它返回范围从 1000 到 9999 的年份值和范围在 0 到 53 之间的周值。
句法 :
YEARWEEK(date, mode)
参数:此方法接受上面提到和下面描述的两个参数:
- date :我们要从中提取年和周的日期或日期时间。
- mode :它指定一周从哪一天开始。下表描述了 mode 参数的工作原理。
Mode | First day of week | Range | Week 1 is the first week … |
---|---|---|---|
0 | Sunday | 0-53 | with a Sunday in this year |
1 | Monday | 0-53 | with 4 or more days this year |
2 | Sunday | 1-53 | with a Sunday in this year |
3 | Monday | 1-53 | with 4 or more days this year |
4 | Sunday | 0-53 | with 4 or more days this year |
5 | Monday | 0-53 | with a Monday in this year |
6 | Sunday | 1-53 | with 4 or more days this year |
7 | Monday | 1-53 | with a Monday in this year |
返回:它同时返回年和周的值。
示例 1:在 2020 年 9 月 28 日使用 Year()函数查找当前年份和星期。
SELECT YEARWEEK(NOW()) AS Current_YearWeek;
输出 :
+------------------+
| Current_YearWeek |
+------------------+
| 202039 |
+------------------+
1 row in set (0.00 sec)
因此,当前年份是 2020 年,周数是 39。
示例 2:使用 YEARWEEK()函数从给定日期时间查找年和周。
SELECT YEARWEEK('2018-04-22 08:09:22') AS Year_Week ;
输出 :
+-----------+
| Year_Week |
+-----------+
| 201816 |
+-----------+
因此,在此示例中,年份为 2018 年,周数为 16。
示例 3:当日期为 NULL 时,使用 YEARWEEK()函数从给定的日期时间查找年和周。
SELECT YEARWEEK(NULL) AS Year_Week ;
输出 :
+-----------+
| Year_Week |
+-----------+
| NULL |
+-----------+
示例 4:在此示例中,我们将查找一年中每周注册课程的学生人数。为了演示创建一个名为的表。
课程 :
CREATE TABLE Course(
Course_name VARCHAR(100) NOT NULL,
Student_id INT NOT NULL,
Student_name VARCHAR(100) NOT NULL,
Enroll_Date Date NOT NULL,
PRIMARY KEY(Student_id)
);
现在向 Product 表插入一些数据:
INSERT INTO
Course(Course_Name, Student_id, Student_name, Enroll_Date)
VALUES
( 'CS101', 161011, 'Amit Singh', '2019-11-26' ),
( 'CS101', 161029, 'Arun Kumar', '2019-11-30' ),
( 'CS101', 161031, 'Sanya Jain', '2019-12-08' ),
( 'CS101', 161058, 'Riya Shah', '2019-12-15' ),
( 'CS101', 162051, 'Amit Sharma', '2019-12-18' ),
( 'CS101', 161951, 'Sayan Singh', '2019-12-26' ),
( 'CS101', 167051, 'Rishi Jana', '2020-01-02' ),
( 'CS101', 168001, 'Aniket Dravid', '2020-01-10' ),
( 'CS101', 168051, 'Rita Singh', '2020-01-13' ),
( 'CS101', 166051, 'Kalyan Ghandi', '2020-01-26' ) ;
所以,我们的表看起来像:
mysql> select * from Course;
+-------------+------------+---------------+-------------+
| Course_name | Student_id | Student_name | Enroll_Date |
+-------------+------------+---------------+-------------+
| CS101 | 161011 | Amit Singh | 2019-11-26 |
| CS101 | 161029 | Arun Kumar | 2019-11-30 |
| CS101 | 161031 | Sanya Jain | 2019-12-08 |
| CS101 | 161058 | Riya Shah | 2019-12-15 |
| CS101 | 161951 | Sayan Singh | 2019-12-26 |
| CS101 | 162051 | Amit Sharma | 2019-12-18 |
| CS101 | 166051 | Kalyan Ghandi | 2020-01-26 |
| CS101 | 167051 | Rishi Jana | 2020-01-02 |
| CS101 | 168001 | Aniket Dravid | 2020-01-10 |
| CS101 | 168051 | Rita Singh | 2020-01-13 |
+-------------+------------+---------------+-------------+
10 rows in set (0.00 sec)
现在,我们将找到每周和每年参加课程的学生人数。
SELECT
YEARWEEK(Enroll_Date) YearandWeek,
COUNT(Student_id) Student_Enrolled
FROM
Course
GROUP BY YEARWEEK(Enroll_Date)
ORDER BY YEARWEEK(Enroll_Date);
输出 :
+-------------+------------------+
| YearandWeek | Student_Enrolled |
+-------------+------------------+
| 201947 | 2 |
| 201949 | 1 |
| 201950 | 2 |
| 201951 | 1 |
| 201952 | 1 |
| 202001 | 1 |
| 202002 | 1 |
| 202004 | 1 |
+-------------+------------------+
8 rows in set (0.00 sec).