且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过在MySQL的另一个表中创建行来选择表中的列

更新时间:2023-11-17 14:15:22

select item_id, price,
       (min(case when tax_name = 'VAT' then tax end)) vat,
       (min(case when tax_name = 'LBT' then tax end)) lbt,
       coalesce(min(case when tax_name = 'VAT' then tax end),0) +
       coalesce(min(case when tax_name = 'LBT' then tax end),0) +
       price total
  from 
      (select a.item_id item_id,
              c.tax_name tax_name,
              (c.tax_value * b.price / 100) tax,
              b.price price
         from item_tax a inner join item_master b on a.item_id = b.item_id
                         inner join tax_master c on a.tax_id = c.tax_id) as calc
 group by item_id, price;

演示此处.