
- 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 select yesterday's date in MySQL?
To select yesterday’s date, use the subdate() function from MySQL. The syntax is as follows
select subdate(yourDatetimeColumnName) as anyVariableName from yourTableName;
To understand the above syntax, let us create a table
mysql> create table YesterdayDateDemo -> ( -> VisitedDateTime datetime -> ); Query OK, 0 rows affected (0.59 sec)
Let us now insert date in the table using insert command. The query is as follows
mysql> insert into YesterdayDateDemo values(now()); Query OK, 1 row affected (0.15 sec) mysql> insert into YesterdayDateDemo values('2012-12-26 13:24:35'); Query OK, 1 row affected (0.17 sec) mysql> insert into YesterdayDateDemo values('2013-10-22 12:20:32'); Query OK, 1 row affected (0.16 sec)
Let us now display all records from the table using select command. The query is as follows
mysql> select *from YesterdayDateDemo;
The following is the output
+---------------------+ | VisitedDateTime | +---------------------+ | 2018-12-18 14:22:48 | | 2012-12-26 13:24:35 | | 2013-10-22 12:20:32 | +---------------------+ 3 rows in set (0.00 sec)
Now here is the query to select yesterday’s date for the dates inserted above using subdate() method from MySQL. The query is as follows
mysql> select subdate(VisitedDateTime,interval 1 day) as YesterdayDate from YesterdayDateDemo;
The following is the output
+---------------------+ | YesterdayDate | +---------------------+ | 2018-12-17 14:22:48 | +---------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to select date from timestamp in MySQL?
- How to select a date less than the current date with MySQL?
- How to select only MySQL date from datetime column?
- MySQL query to select date >= current date - 3 weeks?
- MySQL query to select date from timestamp?
- MySQL query to select date from 00:00 to today’s date
- Select timestamp as date string in MySQL?
- MySQL query to select closest date from today?
- Select date from MySQL and format to text?
- Best way to change the date format in MySQL SELECT?
- MySQL query to select records with a particular date?
- Get timestamp date range with MySQL Select?
- How to select a specific record from MySQL if date is in VARCHAR format?
- How to select rows in MySQL that are >= 1 DAY from the current date?
- MySQL Select Date Equal to Today and return results for the same date?

Advertisements