如何有两个表
1.doner_record - >
Id| doner_id|blood_group | units |status|
1 | 1 |A | 3 |0 |
2 | 3 |B | 2 |0 |
3 | 2 |A | 1 |0 |
4 | 1 |A | 2 |0 |
2。接受者
Id| name |blood_group | units |status|
1 | ayush |A | 1 |0 |
2 | akash |B | 2 |0 |
3 | Hari |A | 1 |0 |
4 | Ram |A | 1 |0 |
如何使用sql
获取血型库存|blood_group | units |
|A | 3 |
|B | 0 |
答案 0 :(得分:0)
对于Stocked Blood of Blood,您需要从接受的单位总和中检查左侧单位。
检查此查询可能会得到结果:
select blood_group,(ifnull(t1.unit,0) - ifnull(t2.unit,0)) as StockedUnit from
(select blood_group,sum(unit) unit from
doner_record group by blood_group) t1
left outer join
(select blood_group,sum(unit) unit from
acceptors group by blood_group) t2
on t1.blood_group = t2.blood_group