mysql having和where的区别

where不能筛选通过函数计算出来的列。

统计消费过100元以上的用户:

错误1

select userid, ordernum, sum(price) from orders group by (userid) where sum(price) > 100;

错误2(设置别名也不行)

select userid, ordernum, sum(price) as spent from orders group by (userid) where spent > 100;

正确:

select userid, ordernum, sum(price) from orders group by (userid) having sum(price) > 100;

发表评论

电子邮件地址不会被公开。 必填项已用*标注