Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL:utilizzo di alias come colonna

Prendi in considerazione l'utilizzo di una sottoquery. Ho aggiunto alias di tabella per rendere la query più leggibile:

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