![](/img/trans.png)
[英]How to combine rows from two tables into a single table based on Foreign key, in MySQL.?
[英]MySQL. How to combine or match specifics rows from two tables
首先,標題不是很清楚。 通過這個例子,我想知道是否有 SQL 代碼的解決方案,或者它是否必須在 C#、Java、PHP 等的另一端工作。
原理是這樣的:
ID Name Amount
1 AA 10
2 BB 9
3 CC 8
4 DD 1
5 ZZ 2
ID Name Fouls
1 BB 4
2 ZZ 1
Name Diff
AA 10
BB 5
CC 8
DD 1
ZZ 1
可以直接用SQL來完成嗎?
你可以left join
:
select i.name, i.amount - coalesce(o.fouls, 0) diff
from inputs i
left join outputs o on o.name = i.name
我認為你只想要一個left join
和算術:
select i.id, i.name, i.amount - coalesce(o.fouls, 0)
from inputs i left join
outputs o
using (id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.