2016-11-23 19 views
2

それぞれに3つのプロパティを含む2つのテーブルがあります。 table 1のすべてのレコードを表示し、table 2には、table 1に存在しないレコードのみを抽出します。テーブル1のレコードとテーブル1のテーブル2からのレコード

表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

いつも1つの答え、より多くを助けた人を受け入れることを忘れないでください! –

+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サーバーを除いて使用することができた、私はそれを知らなかった – 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 
関連する問題