0

I tried to simplify my long queries with user defined variables in Mysql. Unfortunately, these variables don't behave in where clause as expected - especially variables where is stored date. Here are 3 simplified queries:

SELECT id,
       @var_date := `date`
FROM   aroma_exchanges
WHERE  @var_date >= "2000-01-01" 

This returns 0 rows

Another one:

SELECT id,
       @var_date := `date`
FROM   aroma_exchanges
WHERE  Str_to_date(@var_date, "%y-%m-%d") >= "2000-01-01" 

This return 0 rows as well.

SELECT id, 
       @var_date := `date` 
FROM   aroma_exchanges 
WHERE `date` >= "2000-01-01" 

This one works as expected, returns a lot of rows. In my case the definition of date is very complex and in my real query I used it few times so I don't want to repeat the same subquery 5-10 times if I can store it into one variable and use it as I need to. Question: how to make the first or second query to work properly? Thanks.

mesnicka
  • 2,458
  • 8
  • 23
  • 31

0 Answers0