Sử dụng hàm DAYOFWEEK() (Chủ nhật=1, Thứ 2=2, ..., Thứ 7=7) để lọc theo ngày trong tuần.
Tạo bảng và dữ liệu
mysql> CREATE TABLE DemoTable785(
CustomerId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
CustomerName VARCHAR(100),
ShoppingDate DATE
);
Query OK, 0 rows affected (0.61 sec)
mysql> INSERT INTO DemoTable785(CustomerName, ShoppingDate) VALUES
('Chris','2019-07-03'), ('Robert','2019-07-01'),
('David','2019-07-06'), ('Carol','2019-07-19');
Query OK, 4 rows affected
Lọc ngày Thứ 2 (DAYOFWEEK = 2)
mysql> SELECT * FROM DemoTable785 WHERE DAYOFWEEK(ShoppingDate) = 2;
Kết quả:
+------------+--------------+--------------+ | CustomerId | CustomerName | ShoppingDate | +------------+--------------+--------------+ | 2 | Robert | 2019-07-01 | +------------+--------------+--------------+
Bảng tham chiếu DAYOFWEEK:
| Giá trị | Ngày |
|---|---|
| 1 | Chủ nhật |
| 2 | Thứ 2 |
| 3 | Thứ 3 |
| 4 | Thứ 4 |
| 5 | Thứ 5 |
| 6 | Thứ 6 |
| 7 | Thứ 7 |
Hàm liên quan: WEEKDAY() (Thứ 2=0 ... Chủ nhật=6), DAYNAME() (trả về tên ngày).