gpt4 book ai didi

MySQL:使用别名作为列

转载 作者:可可西里 更新时间:2023-11-01 07:59:40 25 4
gpt4 key购买 nike

我有这个问题:

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”

如何使用别名作为列?

最佳答案

考虑使用子查询。我添加了表别名以使查询更具可读性:

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

关于MySQL:使用别名作为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11179484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com