Condition as a column named using AS "name" in mysql
I am trying to count line that has columns which been repeated (their value) more than x times in mysql, every time I 开发者_如何学运维try to execute this query I get an error :
SELECT DISTINCT
idLogin, count(idLogin ) AS "cou"
FROM
`products` AS t
GROUP BY
idLogin
HAVING
t.cou > 2
why this error is happening
Use this one
SELECT DISTINCT idLogin, count(idLogin ) AS cou FROM `products`
GROUP BY idLogin HAVING cou > 2
you can put that expression count(idLogin )
directly in having clouse....
it will not give any error.. and will be more understable as well....
or else you can do one thing -
select idLogin,cou from (
SELECT DISTINCT idLogin, count(idLogin ) AS cou
FROM products
GROUP BY idLogin ) t
where t.cou >2
Remove the double quotes from the aliased column name.
MySQL uses single back-quotes for escaping table and column names, not double quotes.
The correlation name t
does not have a column cou
. Just use this:
HAVING cou > 2
PS: DISTINCT
is redundant in your query. The GROUP BY
ensures there will only be one row per distinct value of idLogin
.
精彩评论