Error reading Like with Timestampdiff

Asked

Viewed 42 times

2

Good People Visual Studio can not read the column Days to make a search with like and is a mistake! I believe he reads the code at once and does not find Dias created with TIMESTAMPDIFF(DAY, Data_de_hoje, Data_do_compromise).
How could I organize the above code for it to do some research and show the result without giving error?

"SELECT ID, Nome_do_cliente, Data_do_compromisso, Data_de_hoje,
  Data_do_cadastro, Data_de_hoje, TIMESTAMPDIFF(DAY, Data_de_hoje, 
  Data_do_compromisso) AS Dias FROM agenda WHERE CONCAT(`Nome_do_cliente`, 
 `Data_do_compromisso`, `Data_de_hoje`, `Dias`)LIKE '%" + valorpesreb + "%'";

Error message:

Data reading error! Unknown Column 'Dias' in Where Clause

1 answer

0

You can only use column nicknames on GROUP BY, ORDER BY or HAVING.

The SQL standard does not allow you to refer to a column alias in a clause WHERE. This restriction is imposed because when the code WHERE is executed, column value may not yet be determined.

Source: http://dev.mysql.com/doc/refman/5.7/en/problems-with-alias.html

Therefore, to solve your problems we will have to duplicate the line of timestampdiff in the where, because we cannot use Alias. Doing the following:

SELECT id
      ,nome_do_cliente
      ,data_do_compromisso
      ,data_do_cadastro
      ,data_de_hoje
      ,timestampdiff(DAY, data_de_hoje, data_do_compromisso) dias

  FROM agenda
 WHERE CONCAT(nome_do_cliente
             ,data_do_compromisso
             ,data_de_hoje
             ,timestampdiff(DAY, data_de_hoje, data_do_compromisso)) LIKE
              '%" + valorpesreb + "%';

I performed the test on Sqlfindle.

  • @Rafael, that way doesn’t solve your problem?

Browser other questions tagged

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