Tags
PHP , MySQL
Asked 8 years ago
22 Nov 2016
Views 1322
sqltreat

sqltreat posted

get result of yesteraday 's date in MySQL?

How to get result of yesterday ' date by sql query in MySQL ?

Database table name : users
Field list : Id(Primary Key) , username , password , register_date (field type : dateTime)

i want to get all user who registered yesterday

select * from users where register_date = 'yesterday'


i can put static date here but i need dynamic date . it means i need registered user for yesterday.
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

you can use subdate(CURRENT_DATE, 1)
subdate means it minus date from the given date . CURRENT_DATE is constant of MySQL for current date. and we are passing 1 as second argument so it will minus 1 day from current date and it give us yesterday date , it return yyyy-mm-dd


select * from users where register_date = subdate(CURRENT_DATE, 1) 


should work but you have register_date is dateTime Field type so you should convert register_date to date by DATE function of MySQL

so it will be

select * from users where DATE(register_date)= subdate(CURRENT_DATE, 1) 


Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00

you can use DATEDIFF function also


SELECT * FROM `users` where DATEDIFF(register_date,CURRENT_DATE)=-1

=-1 because it give minus value for yesterday - today
or


SELECT * FROM `users` where DATEDIFF(CURRENT_DATE,register_date)=1

=1 because it give minus value for today - yesterday
ravi

ravi
answered Nov 30 '-1 00:00

CURRENT_DATE()-1 = yesterdady
use it like this

select * from users where DATE(register_date)=CURRENT_DATE()-1
shyam

shyam
answered Nov 30 '-1 00:00

you can add your date + 1 day and match it with current date

select * from `users` where  DATE(register_date)+1 =CURRENT_DATE()


or you can use interval to subtract 1 day from current date.

SELECT * FROM `users` where DATE(register_date)=CURRENT_DATE - INTERVAL 1 DAY

Post Answer