2016-11-23 84 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 
相关问题