组合两个表而不覆盖 col

时间:2021-06-07 20:26:04

标签: sql sql-server tsql

如何以给定的方式将 tbl2tbl1 结合起来? 我尝试使用 coalesce 解决这个问题,但在空值的情况下,它会在第一个参数上使用第二个参数。这里的任何帮助对我都非常有用。

tbl1:

+-------+-------+-------+-------+-------+------+---------------+----------+
|   id  | fname | mname | lname |  age  | addr |     email     | location |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   1.  |   aa  |   a   |  aaa  |   15  | usa  |  aa@gmail.com |    us    |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   2.  |   bb  |   b   |  bbb  |   22  |      |  bb@gmail.com |    uk    |
+-------+-------+-------+-------+-------+------+---------------+----------+

tbl2:

+-------+-------+------+-------+
|   id  |  age  | addr |  zip  |
+-------+-------+------+-------+
|   4.  |   55  |  hk  |  4566 |
+-------+-------+------+-------+
|   6.  |   43  |  ch  |  5444 |
+-------+-------+------+-------+

期望的输出:

+-------+-------+-------+-------+-------+------+---------------+----------+
|   id  | fname | mname | lname |  age  | addr |     email     | location |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   1.  |   aa  |   a   |  aaa  |   15  | usa  |  aa@gmail.com |    us    |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   2.  |   bb  |   b   |  bbb  |   22  |      |  bb@gmail.com |    uk    |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   4.  |       |       |       |   55  |  hk  |               |          |
+-------+-------+-------+-------+-------+------+---------------+----------+
|   6.  |       |       |       |   43  |  ch  |               |          |
+-------+-------+-------+-------+-------+------+---------------+----------+

我尝试过的(有点不愿意把它放在这里,因为我对此没有信心):

select 
    coalesce(t1.id, t2.d) as id, 
    t1.fname, t1.mname,t1.fname...so on,
    coalesce(t1.age, t2.age) as age, 
    coalesce(t1.addr,t2.addr), 
    t1.email, t1.location 
from 
    tbl1 t1 
full join 
    tbl2 t2 on t1.id = t2.id

2 个答案:

答案 0 :(得分:1)

用 NULL 或空字符串替换缺失的列并联合它们。

UNION 的列数必须相同,否则会报错

<块引用>
CREATE TABLE tbl1
    ("id" int, "fname" varchar(2), "mname" varchar(1), "lname" varchar(3), "age" int, "addr" varchar(3), "email" varchar(12), "location" varchar(2))
;
    
INSERT INTO tbl1
    ("id", "fname", "mname", "lname", "age", "addr", "email", "location")
VALUES
    (1, 'aa', 'a', 'aaa', 15, 'usa', 'aa@gmail.com', 'us'),
    (2, 'bb', 'b', 'bbb', 22, NULL, 'bb@gmail.com', 'uk')
GO

2 行受影响

<块引用> <块引用>
CREATE TABLE tbl2
    ("id" int, "age" int, "addr" varchar(2), "zip" int)
;
    
INSERT INTO tbl2
    ("id", "age", "addr", "zip")
VALUES
    (4, 55, 'hk', 4566),
    (6, 43, 'ch', 5444)
;
GO

2 行受影响

<块引用> <块引用>
SELECT 
id, fname, mname, lname, age, addr, email, location
FROM tbl1
UNION
SELECT
id,NULL,NULL,NULL, age, addr, NULL,NULL
FROM tbl2
GO
id | fname | mname | lname | age | addr | email        | location
-: | :---- | :---- | :---- | --: | :--- | :----------- | :-------
 1 | aa    | a     | aaa   |  15 | usa  | aa@gmail.com | us      
 2 | bb    | b     | bbb   |  22 | null | bb@gmail.com | uk      
 4 | null  | null  | null  |  55 | hk   | null         | null    
 6 | null  | null  | null  |  43 | ch   | null         | null    
<块引用>
select 
    coalesce(t1.id, t2.id) as id, 
    t1.fname
    , t1.mname
    ,t1.fname
    , t1.lname
    ,coalesce(t1.age, t2.age) as age 
    ,coalesce(t1.addr,t2.addr) 
    ,t1.email
    , t1.location 
from 
    tbl1 t1 
FULL OUTER join 
    tbl2 t2 on t1.id = t2.id
GO
id | fname | mname | fname | lname | age | (No column name) | email        | location
-: | :---- | :---- | :---- | :---- | --: | :--------------- | :----------- | :-------
 1 | aa    | a     | aa    | aaa   |  15 | usa              | aa@gmail.com | us      
 2 | bb    | b     | bb    | bbb   |  22 | null             | bb@gmail.com | uk      
 4 | null  | null  | null  | null  |  55 | hk               | null         | null    
 6 | null  | null  | null  | null  |  43 | ch               | null         | null    

db<>fiddle here

答案 1 :(得分:0)

试试这个:

DECLARE     @Table1 TABLE 
            (id int, fname varchar(20), mname varchar(20), lname varchar(20), age int, addr varchar(20), email varchar(20), location varchar(20));
INSERT INTO @Table1 
    VALUES  
            (1, 'aa',   'a',    'aaa',  15, 'usa',  'aa@gmail.com', 'us')
        ,   (2, 'bb',   'b',    'bbb',  22, ''   ,  'bb@gmail.com', 'uk')
;

DECLARE     @Table2 TABLE 
            (id int, age int, addr varchar(20), zip varchar(20));
INSERT INTO @Table2 
    VALUES  
            (4, 55, 'hk',   4566)
        ,   (6, 43, 'ch',   5444)
;
WITH    IDTable AS
    (
        SELECT  ID  FROM    @Table1 UNION ALL
        SELECT  ID  FROM    @Table2 
    )

SELECT  DISTINCT
        id          =   I.id
    ,   fname       =   T.fname 
    ,   mname       =   T.mname 
    ,   lname       =   T.lname 
    ,   age         =   T.age       
    ,   addr        =   T.addr      
    ,   email       =   T.email 
    ,   location    =   T.location  
FROM    IDTable I
JOIN    @Table1 T   ON  T.ID = I.ID
UNION ALL
SELECT  
        id          =   I.id
    ,   fname       =   ''
    ,   mname       =   ''
    ,   lname       =   ''
    ,   age         =   T.age       
    ,   addr        =   T.addr      
    ,   email       =   ''
    ,   location    =   ''  
FROM    IDTable I
JOIN    @Table2 T   ON  T.ID = I.ID
相关问题