I use a mysql database and have the following table: Contract [ begin_date / end_date / title ]
Now i can search and filter of all 3 fields (begin / end / title) and calculate like:
List = Contract.select(“*, TIMESTAMPDIFF(year, begin_date, DATE_ADD(end_date,INTERVAL 3 DAY)) AS contract_lifetime, TIMESTAMPDIFF(month, begin_date, now()) AS lifetime”)
output are now my 3 fields + the 2 calculated fields “lifetime” and “contract_lifetime”.
I can use where clause like:
List = List.where(“begin_date >= ?”, date)
But i can’t use the 2 calculated fields in where clause like:
List = List.where(“lifetime >= ?”, lifetime)
Mysql2::Error: Unknown column 'lifetime' in 'where clause': SELECT *, TIMESTAMPDIFF(year, begin_date, DATE_ADD(end_date,INTERVAL 3 DAY)) AS contract_lifetime, TIMESTAMPDIFF(month, begin_date, now()) AS lifetime FROM `contracts` WHERE (begin_date >= '2020-12-01 00:00:00') AND (begin_date <= '2020-12-03 23:59:59.999999') AND (contract_lifetime = 1)
Why does this not work?