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

I have three tables.

tax_master

item_master

item_tax

The values in it are like this.

*tax_master*

tax_id tax_name tax_value

--------------------------

1 Vat 5

2 LBT 8

*item_master*

item_id Prise

---------------

1 30

2 100

*item_tax*

item_tax_id item_id tax_id

------------------------------

1 1 1

2 2 2

3 1 2

Now i want output like this.

item_id prise VAT LBT Total_prise

---------------------------------------

1 30 1.5 2.4 33.9

2 100 - 8 108

VAT value is calculated like 5/30*100 like 5% on 30=1.5

解决方案

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;

Demo here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值