Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 合并两个表并根据表顺序拾取数据_Sql_Sql Server_Join - Fatal编程技术网

Sql 合并两个表并根据表顺序拾取数据

Sql 合并两个表并根据表顺序拾取数据,sql,sql-server,join,Sql,Sql Server,Join,我正在尝试将2021 tbl与2020表格合并。如果同一记录(id)在两个表中通用,则我选择2021表,如果2021年不存在id,则选择2020年,如果021和020中不存在id,则选择020记录。我试图用coalesce解决这个问题,但没有任何帮助。任何关于改善这种情况的建议 2021_table |num| name | location | age | gender | foundation | relation | email | | ---|------|----------|---

我正在尝试将2021 tbl与2020表格合并。如果同一记录(id)在两个表中通用,则我选择2021表,如果2021年不存在id,则选择2020年,如果021和020中不存在id,则选择020记录。我试图用coalesce解决这个问题,但没有任何帮助。任何关于改善这种情况的建议

2021_table

|num| name | location | age | gender | foundation | relation | email |
| ---|------|----------|-----|--------|------------|----------|--------
| 1 | ally |  texas   | 55  | F | NA  | mom | null          |
|2  | rick |  newyork | 45  | M | NA  | dad | rick@pronet.com|
|3  | macy |  cali    | 66  | F | EY  | na  | macy@yahoo.com|
|8A | steve|  ark     | 39  | M | JJ  | xyz | stev@gmail.com|
 

 2020_table
 
| num| name | location | gender | org | status | descent |
| ----|------|----------|-----|--------|-----|--------|-------|
|1  | ally |  texas   |  F  |  |  |
|2  | r    |  newyork |  M  |  |  |
|78 | naomy|  NULL    |  F  |  |  |
|B12| romy | CO       |  M  |  |  |
 

Output_table:

| num| name | location | age | gender | foundation | relation | email |
| ----|------|----------|-----|--------|------------|----------|---|
| 1  | ally |  texas   | 55  | F | NA  | mom | null           |
| 2  | rick |  newyork | 45  | M | NA  | dad | rick@pronet.com|
|3   | macy |  cali    | 66  | F | EY  | na  | macy@yahoo.com |
|8A  | steve|  ark     | 39  | M | JJ  | xyz | stev@gmail.com |
| 78 | naomy|  NULL    | 30  | F |     |     |                |
| B12| romy | CO       | 69  | M |     |     |                |

您可以使用
union all
exists

select t2021.num, t2021.name, t2021.location, t2021.age, t2021.gender, t2021.foundation, t2021.relation, t2021.email
from table_2021 t2021
union all
select t2020.num, t2020.name, t2020.location, t2020.age, t2020.gender, NULL, NULL, NULL
from table_2020 t2020
where not exists (select 1
                  from table_2021 t2021
                  where t2021.num = t2020.num
                 );

请注意,具有相同列的两个表通常表示您的数据模型有问题。我建议将所有数据放在一个表中,并为该年增加一列。

以下查询应符合规定的规则:

  • 如果id在两个表中通用,则选择2021表
  • 如果该id在2021年不存在,则选择2020年和2020年
  • 如果021和020中不存在id,则选择020记录
这些表不相同,因此我为2020表中不存在的列设置了null。空字符串(“”)也可以使用

SELECT num, name , location , age , gender , foundation , relation , email FROM t2021

UNION 

SELECT num, name , location , age , gender , NULL foundation , NULL relation , NULL email FROM t2020
WHERE num NOT IN  ( SELECT num FROM t2021 )

查看
完全外部连接
。我假设您这样做的原因是为了修复您的设计,因为您不应该在不同的表中存储不同年份的数据。为什么有不同的表?我会考虑一张普通的表格,也许有一年的专栏。都是不同的表格,不同的体裁。这样做的目的是匹配记录,并根据表格年份选择最相关的记录“两者都是不同的表格,不同的类型”。那么,对于不同的年份和类型,您有不同的表格吗?那更糟。应该有一个表格,应该有一个年度和类型的专栏。我明白了,但现在我只能利用我所拥有的(我尝试了选择合并(t1.name,t2.name)作为名称,在所有列上合并,从t1上的t1完全外部联接t2。id=t2.id。您对这种方法有何看法?我无法应用此查询,因为我收到一个错误,指出“联合运算符在目标列表中必须有相等数量的表达式”@New_这里…我明白了。表结构不同。我调整了回答。先生,有一个疑问……这个问题如何将2021年tbl_优先于2020年tbl_?你能explain@New_here…它从2021年开始选择所有内容。从2020年开始只获取不在2021年的ID。瞧!优先级排序。谢谢。如果我使用Union all而不是Union,那么我的输出是否也会包含重复记录?否。重复记录将由not删除在第二个查询的子句中。我使用UNION是因为它具有自动排序功能。但是,您可以对UNION ALL进行测试并找出答案。如果您觉得这很有帮助,请将其标记为“答案并向上投票”。谢谢。