Imagine this query…
SELECT `id`,
`hits` + `other_hits` AS `total_hits`
FROM `something`
WHERE `hits` + `other_hits` > 30
As you can see, I’ve repeated the addition of hits and other_hits. Can I refer to total_hits column I created in other parts of the query?
I tried it, and I got 1054: Unknown column in where clause.
Use:
The earliest MySQL allows references to column aliases is the
GROUP BYclause; clauses after that support references (HAVING,ORDER BY). Most other databases don’t support referencing a table alias before theORDER BY, which typically requires using a derived table/inline view:Otherwise, you have to reuse the logic in the WHERE clause: