select TB_USERINFO.userid as 用户ID,username as 用户名,giftprice as 单价
,sum(quantity) as 数量,总价=case
when giftprice>0 then giftprice*sum(quantity)
else 0
end
from tb_vipbuygift join tb_vip on vip_id=vipid join tb_userinfo on tb_userinfo.userid=tb_vip.userid join tb_gift on tb_gift.giftid=TB_VipBuyGift.giftid group by username,giftprice,TB_USERINFO.userid
就像ID 20用户 购买礼物总价为25000 ------最佳解决方案-------------------- select TB_USERINFO.userid as 用户ID,username as 用户名,总价=sum(case when giftprice>0 then giftprice*quantity else 0 end)
from ...
group by TB_USERINFO.userid,username ------其他解决方案-------------------- 你把giftprice as 单价 去掉,group by的giftprice 也去掉看看 ------其他解决方案--------------------
选择列表中的列 'tb_gift.GiftPrice' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
------其他解决方案--------------------
select 总价=case
when giftprice>0 then giftprice*sum(quantity)
else 0
end
from tb_vipbuygift join tb_vip on vip_id=vipid join tb_userinfo on tb_userinfo.userid=tb_vip.userid join tb_gift on tb_gift.giftid=TB_VipBuyGift.giftid
------其他解决方案--------------------
消息 8120,级别 16,状态 1,第 1 行
选择列表中的列 'tb_gift.GiftPrice' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
------其他解决方案-------------------- select 总价=sum(casewhen giftprice>0 then giftprice*quantity else 0 end) ------其他解决方案--------------------