Use column alias in conditions

With the use of HAVING you can use a result columns of a database query in itself:

SELECT
  category_field = 3 AS 'result'
FROM
  catalog_category_entity
WHERE
  condition_field = 985
HAVING
    result = 1;

Here the alias result (see AS ‘result’) for the only column is used as a condition with HAVING. That is by now the only way to compare a result column with another value in a query under MySQL.

WHERE
    result = 1;

Would not work!!!

Attention: HAVING is slower than a normal WHERE clause!

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.