2017-07-29 2 views
0

J'ai 2 tables et la structure est la même sur les deux tables.T-SQL - Comparer deux tables et afficher le résultat comme disponible sur les deux, table1 seulement et table2 seulement

Tableau 1:

ID Name Phone 
1 xxx  111 
2 yyy  222 

Tableau 2:

ID Name Phone 
1 xxx  111 
3 zzz  333 

je dois comparer ces deux tableaux et afficher les résultats (basé sur ID par colonnes où ID condition) comme

Available in both tables 
Table1 only 
Table2 only 

Ceci devrait être comme ceci,

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

Répondre

0

Cela pourrait fonctionner:

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

Essayez ceci:

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

utilisant HASHBYTES: Demo Here ..vous ne avez pas besoin de consommer la table mutiple fois, mais une seule fois

;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

Vous pouvez utiliser la combinaison de FULL JOIN et IS NULL pour vérifier la disponibilité des deux tables -

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); 

Comme cette requête utilise une seule opération et sans REJOIGNEZ requêtes sous il est très rapide.