如何从第一个表中获取匹配数据并从第二个表中取消匹配

时间:2015-05-27 07:25:00

标签: sql-server-2008

我有两张相同结构的表 例如。表A和B,列id,值。

Table A

ID    VALUE
1      10
2      20
3      0


Table B

ID    VALUE
1      24
2      26
3      0
4      40
5      50

    expected output:

ID    VALUE
1      10
2      20
3      0
4      40
5      50

根据输出,前三个id与表B匹配,因此id(1,2,3)的值来自表A和id(4,5),其值不匹配,因此它来自表B.

1 个答案:

答案 0 :(得分:1)

您可以在表格b(或表格a上的left join)上使用coalesce并使用select b.id, coalesce(a.value, b.value) from tablea a right join tableb b on a.id = b.id 运算符。

select b.id, coalesce(a.value, b.value)
from tableb b
left join tablea a on a.id = b.id

func (this *ScoreProvider) getScore()(res float64) {
    var score1 int16 = 500
    var score2 int16 = 400
    var score3 int16 = 300
    var score4 int16 = 200
    res = score1 * 0.25 + score2 * 0.25 + score3 * 0.25 + score4 * 0.25

    return
}

请参阅SqlFiddle

相关问题