Select a column if condition is met in MySQL to fetch records from current date and current date + 1

MySQLMySQLi Database

Let us first get the current date −

mysql> select curdate();

This will produce the following output −

+------------+
| curdate()  |
+------------+
| 2019-12-15 |
+------------+
1 row in set (0.00 sec)

Let us first create a table −

mysql> create table DemoTable1956
   (
   ProductId int,
   ProductName varchar(20),
   CustomerName varchar(20),
   ShippingDate date
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1956 values(101,'Product-1','Sam','2019-10-11');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(102,'Product-2','Carol','2018-12-01');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(103,'Product-3','David','2019-12-15');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1956 values(104,'Product-4','Robert','2019-12-16');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1956;

This will produce the following output −

+-----------+-------------+--------------+--------------+
| ProductId | ProductName | CustomerName | ShippingDate |
+-----------+-------------+--------------+--------------+
|       101 | Product-1   | Sam          | 2019-10-11   |
|       102 | Product-2   | Carol        | 2018-12-01   |
|       103 | Product-3   | David        | 2019-12-15   |
|       104 | Product-4   | Robert       | 2019-12-16   |
+-----------+-------------+--------------+--------------+
4 rows in set (0.00 sec)

Here is the query to select a column and fetch current date and next date records −

mysql> select CustomerName from DemoTable1956 where ShippingDate IN(CURDATE(),CURDATE()+interval 1 Day);

This will produce the following output −

+--------------+
| CustomerName |
+--------------+
| David        |
| Robert       |
+--------------+
2 rows in set (0.00 sec)
raja
Published on 31-Dec-2019 11:34:18
Advertisements