How can I select rows which fall on a specific day of week in MySQL?


For specific day of week, use DAYOFWEEK().

Let us first create a table −

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)

Insert some records in the table using insert command −

mysql> insert into DemoTable785(CustomerName,ShoppingDate) values('Chris','2019-07-03');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable785(CustomerName,ShoppingDate) values('Robert','2019-07-01');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable785(CustomerName,ShoppingDate) values('David','2019-07-06');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable785(CustomerName,ShoppingDate) values('Carol','2019-07-19');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable785;

This will produce the following output -

+------------+--------------+--------------+
| CustomerId | CustomerName | ShoppingDate |
+------------+--------------+--------------+
| 1          | Chris        | 2019-07-03   |
| 2          | Robert       | 2019-07-01   |
| 3          | David        | 2019-07-06   |
| 4          | Carol        | 2019-07-19   |
+------------+--------------+--------------+
4 rows in set (0.00 sec)

Following is the query to select rows which fall on a specific day of week −

mysql> select *from DemoTable785 where DAYOFWEEK(ShoppingDate)=2;

This will produce the following output -

+------------+--------------+--------------+
| CustomerId | CustomerName | ShoppingDate |
+------------+--------------+--------------+
| 2          | Robert       | 2019-07-01   |
+------------+--------------+--------------+
1 row in set (0.00 sec)
raja
Published on 09-Sep-2019 12:01:15
Advertisements