themasmul themasmul - 3 months ago 10
MySQL Question

Mysql Add DATE_COLUMN 3 DAYS with query

I've been asking many question here, but it was dead end, I just want to make a simple select from date_column+3 day, I'm new to MySQL.

I've tried with this query

SELECT *
FROM pemesanan
WHERE pemesanan.`date` = DATE(DATE_ADD(date, INTERVAL 3 DAY))


but the result is empty, here's my column

date
2016-08-10
2016-08-04
2016-08-07


it must be show the 2016-08-10 data, but its not,

Can somebody enlighten me, I've been frustrated with this

Answer

If you use date right side you should use date left side

  SELECT * 
  from pemesanan 
  where  date(pemesanan.`date`) = DATE(DATE_ADD(NOW(), INTERVAL 3 DAY))

If you use the same date pemesanan.date you never get pemesanan.date = pemesanan.date +3

but if you want select the date older than 3 day you shuold use

  SELECT * 
  from pemesanan 
  where  date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))

and for delete

  DELETE from pemesanan 
  where  date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))

in your case

  SELECT * 
  from pemesanan 
  where  date(pemesanan.tanggal)  <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
Comments