请问我的SQL错在哪了?应该怎样改?
select name1,sum(sale*quantity) as aa
from shop_jb a join shop_xs b on a.id=b.id
where aa> 3000
group by name1 having count(b.id)> 2
运行结果是
服务器: 消息 207,级别 16,状态 3,行 2
列名 'aa ' 无效。
服务器: 消息 207,级别 16,状态 1,行 2
列名 'aa ' 无效。
where 后面不能跟别名吗?请问我错哪了,应该怎样改?
多谢各位!!
------解决方案--------------------select name1,sum(sale*quantity) as aa
from shop_jb a join shop_xs b on a.id=b.id
group by name1 having count(b.id)> 2 and sum(sale*quantity)> 3000
--aa为别名
------解决方案--------------------select name1,sum(sale*quantity) as aa
from shop_jb a join shop_xs b on a.id=b.id
where sum(sale*quantity)> 3000
group by name1 having count(b.id)> 2
别名是不可以做为条件来使用的
------解决方案--------------------或
select name1,aa
from
(select name1,sum(sale*quantity) as aa
from shop_jb a join shop_xs b on a.id=b.id
group by name1 having count(b.id)> 2)tmp
where aa> 3000
------解决方案--------------------select name1,sum(sale*quantity) as aa
from shop_jb a join shop_xs b on a.id=b.id
group by name1
having count(b.id)> 2 and sum(sale*quantity)> 3000 --这里不能用别名aa