2017-07-29 7 views
0

2つのテーブルがあり、両方のテーブルで構造が同じです。T-SQL - 2つのテーブルを比較し、結果を両方で利用可能、テーブル1のみ、テーブル2のみ表示する

表1:

ID Name Phone 
1 xxx  111 
2 yyy  222 

表2:

ID Name Phone 
1 xxx  111 
3 zzz  333 

Iは、これらの2つのテーブルを比較し

Available in both tables 
Table1 only 
Table2 only 
として(条件ID IDのカラム・基づく)結果を表示するために必要

これは次のようになります。

ID Name Phone Status 
---------------------------------------- 
1 xxx  111 Available in both 
2 yyy  222 Available in T1 only 
3 zzz  333 Available in T2 only 

答えて

0

これは仕事ができる:

CREATE TABLE #T1 (ID INT, Name VARCHAR(10), Phone VARCHAR(10)) 
CREATE TABLE #T2 (ID INT, Name VARCHAR(10), Phone VARCHAR(10)) 

INSERT INTO #T1 VALUES (1, 'xxx', '111'), (2, 'yyy', '222') 
INSERT INTO #T2 VALUES (1, 'xxx', '111'), (3, 'zzz', '333') 

SELECT #T1.ID, #T1.Name, #T1.Phone, 'Available on both' AS Status 
FROM #T1 INNER JOIN #T2 ON #T1.ID = #T2.ID 
UNION 
SELECT #T1.ID, #T1.Name, #T1.Phone, 'Available on T1 only' AS Status 
FROM #T1 LEFT JOIN #T2 ON #T1.ID = #T2.ID 
WHERE #T2.ID IS NULL 
UNION 
SELECT #T2.ID, #T2.Name, #T2.Phone, 'Available on T2 only' AS Status 
FROM #T1 RIGHT JOIN #T2 ON #T1.ID = #T2.ID 
WHERE #T1.ID IS NULL 

DROP TABLE #T1 
DROP TABLE #T2 
1

はこれを試してみてください:

declare @table1 table 
(
name varchar(10), 
phone varchar(10) 
) 
declare @table2 table 
(
name varchar(10), 
phone varchar(10) 
) 
INSERT INTO @table1 VALUES('xxx','111') 
INSERT INTO @table1 VALUES('yyy','222') 

INSERT INTO @table2 VALUES('xxx','111') 
INSERT INTO @table2 VALUES('zzz','333') 

SELECT t1.name, t1.phone, 'Available on both' FROM 
@table1 t1 INNER JOIN @table2 t2 
ON t1.name = t2.name and t1.phone = t2.phone 
UNION 
SELECT name, phone, 'Available on T1 only' FROM 
@table1 t1 WHERE NOT EXISTS 
(SELECT 1 FROM @table2 t2 
WHERE t1.name = t2.name and t1.phone = t2.phone) 
UNION 
SELECT name, phone, 'Available on T2 only' FROM 
@table2 t2 WHERE NOT EXISTS 
(SELECT 1 FROM @table1 t1 
WHERE t1.name = t2.name and t1.phone = t2.phone) 
2

をHASHBYTESを使用して:Demo Here ..youがテーブルにみとめ回を消費する必要がありますが、一度だけ

;with cte 
as 
(
select id,name,phone,hashbytes('sha1',concat(id,name,phone))as tb1 
from #t1 
) 
select isnull(c.id,b.id) as id, 
     isnull(c.name,b.name) as name, 
     isnull(c.phone,b.phone) as phone, 
case when c.tb1 is null then 'exists in second table only' 
    when c.tb1 is not null and b.tb1 is not null then 'exists in both' 
    when b.tb1 is null then 'exists in first table only' 
    end as 'exidts' from cte c 
full join 
(

select id,name,phone,hashbytes('sha1',concat(id,name,phone))as tb1 
from #t2 
) b 
on 
b.tb1=c.tb1 
いけません
1

FULLはをJOINとは、両方のテーブルから状況を確認するには NULL IS -

SELECT ISNULL(t1.id, t2.id) AS Id 
     , ISNULL(t1.name, t2.name) AS Name 
     , ISNULL(t1.phone, t2.phone) AS Phone 
     , CASE 
      WHEN t1.id IS NULL THEN 'Available in T2 only' 
      WHEN t2.id IS NULL THEN 'Available in T1 only' 
      ELSE 'Available in both' 
     END AS Status 
FROM Table1 AS t1 
FULL JOIN Table2 AS t2 ON (t2.id = t1.id); 

このクエリは一方だけが、それは非常に高速で運転していないサブクエリをJOINを使用するので。

関連する問題