【问题标题】:how to select two table and merge the data not to show the duplicate如何选择两个表并合并数据不显示重复
【发布时间】:2016-10-21 11:25:31
【问题描述】:

是否可以实现如下结果集?

表1:

id_s  name   post_code     city     amount1
------------------------------------------
1     name1  postal1    city1    300
2     name2  postal2    city2    400
3     name3  postal3    city3    NULL
4     name4  postal4    city4    NULL

表2:

id_p  name   post_code     city     Amount2
------------------------------------------
1     name1  postal1    city1    300
2     name2  postal2    city2    NULL
3     name3  postal3    city3    400
5     name5  postal5    city5    500  

结果集:

id_s  name   post_code     city     amount1  amount2
------------------------------------------
1     name1  postal1    city1    300         300
2     name2  postal2    city2    400         NULL
3     name3  postal3    city3    NULL        400
4     name4  postal4    city4    NULL        NULL
5     name5  postal5    city5    NULL        500  

【问题讨论】:

  • 这是一个简单的OUTER JOIN
  • 非常奇怪的桌子设计。为什么将如此相似的数据存储在两个单独的表中?如果 id_p 相同但名称/城市等不同怎么办?
  • 我们可以为您的问题提供正确的解决方案吗?

标签: sql sql-server tsql select


【解决方案1】:

试试这个:

    DECLARE @table1 TABLE
    ( id_s INT, name NVARCHAR(20), post_code NVARCHAR(20), 
city NVARCHAR(20), amount1 NVARCHAR(20) )
    DECLARE @table2 TABLE
    ( id_s INT, name NVARCHAR(20), post_code NVARCHAR(20), 
city NVARCHAR(20), amount2 NVARCHAR(20) )

    INSERT INTO @table1 VALUES
    ('1'     ,'name1',  'postal1',    'city1',    '300'),
    ('2'     ,'name2',  'postal2',   'city2',    '400'),
    ('3'     ,'name3',  'postal3',    'city3',    NULL),
    ('4'    ,'name4',  'postal4',    'city4',    NULL)

    INSERT INTO @table2 VALUES
    ('1',     'name1', 'postal1',    'city1',    '300'),
    ('2',     'name2',  'postal2',    'city2',    NULL),
    ('3',     'name3',  'postal3',    'city3',    '400'),
    ('5',     'name5',  'postal5',    'city5',    '500' )

    SELECT * FROM @table1

    SELECT * FROM @table2  

    SELECT 
    ISNULL(t1.id_s,t2.id_s) id_s, ISNULL(t1.name,t2.name) name,
    ISNULL(t1.post_code,t2.post_code) post_code, ISNULL(t1.city,t2.city) city,
    amount1,amount2
     FROM @table1 t1 FULL JOIN @table2 t2 ON t1.id_s = t2.id_s

这将为您提供完美的结果。

希望对您有所帮助。 :)

【讨论】:

    【解决方案2】:

    full outer join 应该可以完成这项工作:

    SELECT          COALESCE(id_s, id_p),
                    COALESCE(table1.name, table2.name),
                    COALESCE(table1.post_code, table2.post_code),
                    COALESCE(table1.city, table2.city),
                    amount1,
                    amount2
    FROM            table1
    FULL OUTER JOIN table1 ON id_s = id_p
    

    【讨论】:

      【解决方案3】:

      试试这个:

      SELECT
              COALESCE(table_1.id_s, table_2.id_p) as id
             ,COALESCE(table_1.name, table_2.name) as name
             ,COALESCE(table_1.post_code, table_2.post_code) as post_code
             ,COALESCE(table_1.city, table_2.city) as city
             ,table1.amount1
             ,table2.amount2
      FROM table1
      FULL OUTER JOIN table2 ON table_1.id_s = table_2.id_p
      

      外连接: FULL OUTER JOIN 关键字返回左表 (table1) 中的所有行,以及右表 (table2) 中的所有行。如果“table1”中存在与“table2”中不匹配的行,或者“table_2”中存在与“table1”中不匹配的行,也会列出这些行。

      http://www.w3schools.com/sql/sql_join_full.asp

      结合: 按顺序计算参数并返回最初不计算为 NULL 的第一个表达式的当前值。

      https://msdn.microsoft.com/en-us/library/ms190349.aspx

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        相关资源
        最近更新 更多