我有这个问题:
select qa_returns_items.item_code,
(CASE status_code
WHEN 11 THEN (qa_returns_items.item_quantity - qa_returns_residues.item_quantity)
WHEN 12 THEN (qa_returns_items.item_quantity + qa_returns_residues.item_quantity) END) as total_ecuation ,
qa_returns_residues.item_unitprice,
( qa_returns_residues.item_unitprice * total_ecuation) as item_subtotal,
(qa_returns_residues.item_discount * item_quantity) as item_discount,
( ( qa_returns_residues.item_unitprice * total_ecuation) -
(qa_returns_residues.item_discount * item_quantity) ) as item_total
from qa_returns_residues, qa_returns_items
where
total_ecuation > 0
AND qa_returns_items.item_code = qa_returns_residues.item_code;
它向我显示错误:'字段列表'中的未知列'total_ecuation'
如何将别名用作列?
答案 0 :(得分:3)
考虑使用子查询。我添加了表别名以使查询更具可读性:
select *
, item_unitprice * total_ecuation as item_subtotal
, (item_unitprice * total_ecuation) - item_discount as item_total
from (
select ri.item_code
, case status_code
when 11 then ri.item_quantity - rr.item_quantity
when 12 then ri.item_quantity + rr.item_quantity
end as total_ecuation
, rr.item_unitprice
, rr.item_quantity
, rr.item_discount * rr.item_quantity as item_discount
from qa_returns_residues rr
join qa_returns_items ri
on ri.item_code = rr.item_code
) as SubQueryAlias
where total_ecuation > 0
答案 1 :(得分:2)
我担心您必须重写查询以使用命名子查询,或者在where子句中重复整个case语句...