
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to update column values with date records and set 1 for corresponding records before the current date in SQL
Let’s say the current date is 2019-08-20. Now for our example, we will create a table −
mysql> create table DemoTable ( ProductStatus tinyint(1), ProductExpiryDate date ); Query OK, 0 rows affected (1.03 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(0,'2019-06-12'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values(0,'2019-10-11'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(0,'2018-07-24'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(0,'2018-09-05'); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+-------------------+ | ProductStatus | ProductExpiryDate | +---------------+-------------------+ | 0 | 2019-06-12 | | 0 | 2019-10-11 | | 0 | 2018-07-24 | | 0 | 2018-09-05 | +---------------+-------------------+ 4 rows in set (0.00 sec)
Following is the query to set value 1 for records before the current date
mysql> update DemoTable set ProductStatus=1 where ProductExpiryDate <=CURDATE(); Query OK, 3 rows affected (0.95 sec) Rows matched : 3 Changed : 3 Warnings : 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+---------------+-------------------+ | ProductStatus | ProductExpiryDate | +---------------+-------------------+ | 1 | 2019-06-12 | | 0 | 2019-10-11 | | 1 | 2018-07-24 | | 1 | 2018-09-05 | +---------------+-------------------+ 4 rows in set (0.00 sec)
- Related Articles
- MySQL queries to update date records with NULL values
- Select a column if condition is met in MySQL to fetch records from current date and current date + 1
- How to find last date from records with date values in MySQL?
- How to compare the first date and the last date from a MySQL column with date records?
- How to get the difference between date records and the current date in MySQL?
- Fetch records from interval of past 3 days from current date in MySQL and add the corresponding records
- Condition to check for due date and current date records in MySQL where clause
- Comparison of varchar date records from the current date in MySQL
- Fetch student records whose result declared 12 days before the current date in MYSQL
- How to change the date in a table with date records with MySQL?
- Fetch date records comparing with the current date’s day and month in MySQL
- MySQL query to get the current date records wherein one of the columns displays current date
- Find the difference between current date and the date records from a MySQL table
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?
- MySQL query to set current date in the datetime field for all the column values

Advertisements