0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【SQL Server】OracleのMINUS演算子に変わるEXCEPT演算子

Last updated at Posted at 2019-10-01

SQLServerでSELECTの差を取得する。

tableAとtableBがあり、tableAのみに存在するレコード(行)を抽出したいときに使用する。
差集合なので、A - B のベン図を想像すれば分かりやすい。
(AかつBでない)

SELECT * FROM tableA
EXCEPT
SELECT * FROM tableB

tableAのクエリにあって、tableBのクエリにない個別の値が返される。

反対にすれば、、、

SELECT * FROM tableB
EXCEPT
SELECT * FROM tableA

tableBのクエリにあって、tableAのクエリにない個別の値が返される。

結果の並び順を指定したい場合は ORDER BY 句を最後に指定する。
カラム名は左のクエリを指定する。

SELECT tableID
      ,tableName
FROM tableB
EXCEPT
SELECT tableID
      ,tableName
FROM tableA
ORDER BY tableID ASC;

注意点

1.それぞれのクエリの列は同じ数でなければならない。
2.順番も一致していなければならない。
3.データ型が一致していなければならい。
4.暗黙に同じ型に変換可能であること。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?