Select records smaller than the current date and time

Asked

Viewed 1,634 times

4

What is the best way to query the database where the data is smaller than the current date and time

SELECT * FROM agenda WHERE agendamento < '".date('Y-m-d H:m:s')."'

2 answers

5


You can use the function of MySQL.

If your column has time:

SELECT *
  FROM agenda
 WHERE agendamento < NOW()

NOW

Returns the Current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or 'YYYYMMDDHHMMSS' format, Depending on whether the Function is used in a string or Numeric context. The value is Expressed in the Current time zone.

In free translation:

Returns the current date and time as the value in the format 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS, depending on the context in which the function is used (string or numeric). The value is expressed in time zone current.

You can test here!

If your column is date only:

SELECT *
  FROM agenda
 WHERE agendamento < CURDATE()

CURDATE

Returns the Current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, Depending on whether the Function is used in a string or Numeric context.

In free translation:

Returns the current date as the value in the format 'YYYY-MM-DD' or YYYYMMDD, depending on the context in which the function is used (string or numeric).

  • 1

    @Marcospaulo this way you will not need to pass parameter. Simpler and more direct than this is impossible.

  • @Sorack , very simple, however I am working with a company that has version 5.5.53 of Mysql, and from what I know of the staff there, I will not be able to update, because there are other systems working So I discovered this now that I put the query you gave me and gave error You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ''' at line 1

  • @Marcospaulo version 5.5 also has the function NOW. Probably the mistake is in the way you are riding your string with the query

  • puts, was a small slip, was a simple loose quotes, now does not appear the error, but did not return anything

  • @Marcospaulo sticks here as his query

  • I removed the current date and put it apart, so it worked

Show 1 more comment

-2

$today = date("d/m/Y");
$dia = date("d");
$mes = date("m");
$ano = date("Y");
$total = 0;

function selectAllPessoa(){
    $banco = abrirBanco(); 
    $sql = "SELECT * FROM agenda WHERE status = 'N' AND(DAY(data_consulta)='$dia') AND (MONTH(data_consulta)) ORDER BY nome";

I did so for years but in my code stopped fncionar try aí1

Browser other questions tagged

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