表测试:
+--------+--------+-------+------+
| source | target | count | prob |
+--------+--------+-------+------+
| test1 | test2 | 2 | 1 |
| cat | dog | 2 | 1 |
+--------+--------+-------+------+
表测试2:
+--------+--------+-------+------+
| source | target | count | prob |
+--------+--------+-------+------+
| cat | dog | 1 | 0.5 |
| dog | cat | 1 | 0.5 |
+--------+--------+-------+------+
我试图通过代码组合两个表,但失败了
select test.count+test2.count, test.prob+test.prob
from test, test2
where (test.source = test2.source) and (test.target = test2.target);
如果我想像这样更新测试表
+--------+--------+-------+------+
| source | target | count | prob |
+--------+--------+-------+------+
| test1 | test2 | 2 | 1 |
| cat | dog | 3 | 1.5|
| dog | cat | 1 | 0.5|
+--------+--------+-------+------+
有什么建议吗?
最佳答案
尝试 Union 与 group by 。
select source , target , SUM(`count`), SUM(prob )
(select * from test
Union
select * from test2) as t
group by source,target
关于mysql - 合并并更新sql中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30476396/