select acc.user_id
, member.nickname
, sum(credit) as ttl
, member.balance
from account_history as acc
left
join member
ON acc.user_id = member.user_id
where balance > 0
and balance <50
group
by user_id
我想显示 ttl ( sum(credit) ) > 0
,
所以我改成
select acc.user_id,member.nickname, sum(credit) as ttl ,member.balance
from account_history as acc left join member
ON acc.user_id=member.user_id
where balance > 0 and balance <50 and ttl > 0
group by user_id
但它变成了错误。未找到列 ttl
最佳答案
使用 having
子句中的值应该有效:
select acc.user_id,member.nickname, sum(credit) as ttl ,member.balance
from account_history as acc
left join member ON acc.user_id=member.user_id
where balance > 0 and balance <50
group by user_id
having ttl > 0
在 where
子句中你不能使用别名
另见 https://dev.mysql.com/doc/refman/8.0/en/problems-with-alias.html
关于mysql - sql 在查询子句中使用求和结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55351823/