Select from today until a week ago

Asked

Viewed 73 times

0

I want to select a sql date from today until a week ago, I found this code here

'SELECT * FROM tb_user WHERE registro BETWEEN CURRENT_DATE()-7 AND CURRENT_DATE() ORDER BY registro ASC';

more it works only if the database field is of date, mine is as datetime, as I could adapt this code?

1 answer

0


You can do so using DATE_ADD() or DATE_SUB():

    SELECT * FROM tabela
    WHERE coluna_date_time >= DATE_ADD(CURDATE(),INTERVAL -7 DAY);

or

    SELECT * FROM tabela
    WHERE coluna_date_time >= DATE_SUB(CURDATE(),INTERVAL 7 DAY);

Without these functions you can do so:

    SELECT * FROM tabela
    WHERE coluna_date_time >= (CURDATE() + INTERVAL -7 DAY);

or

    SELECT * FROM tabela
    WHERE coluna_date_time >= (CURDATE() - INTERVAL 7 DAY);
  • thank you gave it right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.