Search code examples
mysqlcalculated-columns

How to compute a value from two MySQL database columns for use in WHERE clause


I have a query that starts like...

SELECT session_id, time, quantity, teacher, sum(time+(quantity*60*60*60)) AS end FROM my_table WHERE end > 1582214400 

I am getting a PDO exception...

column not found: 1054 Unknown column 'end' in 'where clause'

Why is PDO/MySQL giving me this error? What is the correct syntax to compute end so I can use it in the WHERE clause?


Solution

  • You can't use an alias from select in where clause. Use having instead as follows:

    SELECT session_id, time, quantity, teacher, sum(time+(quantity*60*60*60)) AS end FROM my_table having end > 1582214400