我又回来了另一个问题。我知道有很多与此问题相关的链接但无法找到我的查询的确切解决方案。这是我的问题: -
SELECT c.cust_id, c.cust_name, c.cust_mob, sum(CASE WHEN trans_type = 'Purchase' THEN total_amt ELSE 0 END) as purchase, sum(CASE WHEN trans_type = 'Sale' THEN total_amt ELSE 0 END) as sale, sum(ifnull(a.payment_amt,0)) as tot_pay, (purchase-(sale+sum(a.payment_amt))) as tot_torcv, ((sale+sum(a.payment_amt))-purchase) as tot_topay FROM bil_customers c
inner join bil_vendor_account a on(c.cust_id=a.vendor_id)
WHERE c.cust_catagory = '3'
group by cust_id
having ifnull(tot_torcv,0) between '0' and '100000'
order by a.sl_no
我不知道查询有什么问题,因为它引发了以下错误: -
1054 - 未知专栏'购买'在'字段列表'
请帮我解决问题。在此先感谢!!
答案 0 :(得分:1)
您的错误是,您使用购买作为列,即使它是别名,您也可以使用此
未经过测试
SELECT c.cust_id, c.cust_name, c.cust_mob, @purchase :=sum(CASE WHEN trans_type = 'Purchase' THEN total_amt ELSE 0 END) as purchase, sum(CASE WHEN trans_type = 'Sale' THEN total_amt ELSE 0 END) as sale, sum(ifnull(a.payment_amt,0)) as tot_pay, (@purchase-(sale+sum(a.payment_amt))) as tot_torcv, ((sale+sum(a.payment_amt))-@purchase) as tot_topay FROM bil_customers c
inner join bil_vendor_account a on(c.cust_id=a.vendor_id)
WHERE c.cust_catagory = '3'
group by cust_id
having ifnull(tot_torcv,0) between '0' and '100000'
order by a.sl_no
如果我错过了什么,请告诉我
答案 1 :(得分:0)
您不能在select语句中使用别名createtd来计算其他列。你需要用计算替换别名
SELECT c.cust_id, c.cust_name, c.cust_mob, sum(CASE WHEN trans_type = 'Purchase' THEN total_amt ELSE 0 END) as purchase, sum(CASE WHEN trans_type = 'Sale' THEN total_amt ELSE 0 END) as sale, sum(ifnull(a.payment_amt,0)) as tot_pay, (sum(CASE WHEN trans_type = 'Purchase' THEN total_amt ELSE 0 END)-(sale+sum(a.payment_amt))) as tot_torcv, ((sale+sum(a.payment_amt))-sum(CASE WHEN trans_type = 'Purchase' THEN total_amt ELSE 0 END)) as tot_topay FROM bil_customers c
inner join bil_vendor_account a on(c.cust_id=a.vendor_id)
WHERE c.cust_catagory = '3'
group by cust_id
having ifnull(tot_torcv,0) between '0' and '100000'
order by a.sl_no
答案 2 :(得分:0)
除了CodeSlays所写的内容之外,<expression> between '0' and '100000'
将不会按预期工作,因为它将执行字符串比较而不是数字比较(提醒:'9'&gt; '10')。