Query not identifying value

2

SQL command:

SELECT message, count(message) as amount
FROM 'messages'
WHERE LENGTH(message) >= 4 AND amount < 30 AND message != '#ddd' AND message != 'ddd' 
GROUP BY message
ORDER BY amount DESC
LIMIT 3

# 1054 - Unknown column 'amount' in 'where clause'

Why does not it identify the amount being defined at the outset?

    
asked by anonymous 08.03.2015 / 17:28

1 answer

2

You can not use a alias in the class WHERE in this case it is necessary to redo the expression again or call the column by the 'original', WHERE does its check line by line so it is not possible to add a value in it what should be done is to group the result with GROUP BY and then do the check with the HAVING clause that does the same thing as WHERE but it works with aggregate results.

SELECT message, count(message) as amount
FROM 'messages'
WHERE LENGTH(message) >= 4 AND message != '#ddd' AND message != 'ddd' 
GROUP BY message
HAVING count(message) < 30
ORDER BY amount DESC
    
08.03.2015 / 17:36