Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using generated column in a MySQL where clause

If I have a query, such as:

select column1, (
    select count(*) from table2
) as some_count
from table1
where column1 = 'foo'

I can use column1 in the where clause, but if I try to add

and some_count > 0

then I get an error that some_count doesn't exist. How can I use that in a where clause?

like image 231
Carson Myers Avatar asked Dec 03 '22 04:12

Carson Myers


1 Answers

Use HAVING

select column1, (
    select count(*) from table2
) as some_count
from table1
HAVING some_count > 0
like image 82
zerkms Avatar answered Dec 19 '22 20:12

zerkms