The where
clause is evaluated before the select
clause, which is why you can't refer to select aliases in your where clause.
You can however refer to aliases from a derived table.
select * from (
select user as u1, url as u2 from rank_test
) t1 where u1 <> "";
select * from (
select user, count(*) as cnt from rank_test group by user
) t1 where cnt >= 2;
Side note: a more efficient way to write the last query would be
select user, count(*) as cnt from rank_test group by user
having count(*) >= 2
If I remember correctly, you can refer to the alias in having
i.e. having cnt >= 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…