2016-11-23 90 views
2

我有兩個表,每個表中包含三個屬性。我想要顯示來自table 1table 2的所有記錄,只提取table 1中不存在的記錄。顯示錶1中的記錄和表2中不存在的記錄1

表1

ID Percentage OrderDate 
+----+------------+----------+ 
    1  2.0  2015-05-08 
    1  5.0  2014-05-08 
    1  19.65  2013-05-08 
    1  5.06  2012-05-08 
    1  98.0  2011-05-08 
    1  8.56  2010-05-08 
+----+------------+----------+ 

表2

ID Percentage OrderDate 
+----+------------+----------+ 
    1  45.5  2015-05-08 
    1  45.23  2014-05-08 
    1  12.00  2013-05-08 
    1  6.45  2012-05-08 
    1  18.0  2011-05-08 
    1  5.2  2010-05-08 
    1  12.0  2009-05-08 
    1  22.78  2008-05-08 
    1  48.9  2007-05-08 
    1  7.89  2006-05-08 
    1  17.96  2005-05-08 
    1  11.3  2004-05-08 
+----+------------+----------+ 
+0

顯示查詢你已經試過嗎?此外,他們被稱爲列。 –

+0

不要忘記總是接受一個答案,幫助更多的人! –

+0

完美我會這樣做,但有時我真的忘記 – Ilyas

回答

0

您可以使用EXCEPT keyword

SELECT ID, Pourcentage, OrderDate 
FROM table1 
UNION 
(
    SELECT ID, Pourcentage, OrderDate 
    FROM table2 
    EXCEPT 
    SELECT ID, Pourcentage, OrderDate 
    FROM table1 
) 
+0

我們可以使用除了在SQL Server中,我不知道 – Ilyas

+0

是的,我推薦SQL 2008相信。 –

+0

我想做一個查詢,其中顯示錶1中的所有行,並且表2中不存在的行不存在 – Ilyas

0

根據其列,您將可以比較你可以使用類似下面。

SELECT t1.ID, t1.Percentage, t1.OrderDate 
FROM table1 t1 
UNION ALL 
SELECT t2.ID, t2.Percentage, t2.OrderDate 
FROM table1 t1 
INNER JOIN table2 t2 ON t2.ID <> t1.ID AND t2.Percentage <> t1.Percentage AND t2.OrderDate <> t1.OrderDate 
0

,如果你想,如果你希望所有從表都使用工會的行刪除重複利用工會

select ID, Percentage, OrderDate 
from table1 
union 
select ID, Percentage, OrderDate 
from table2 

所有

select ID, Percentage, OrderDate 
from table1 
union all 
select ID, Percentage, OrderDate 
from table2 
相關問題