10
4

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.

Postgresqlで二つのSELECT句の差集合を取得したい!

Last updated at Posted at 2016-08-18

PostgreSQLで二つのSELECT句の差集合を取得したい!

某案件で、二つのSELECT句の差集合(Aにいて、Bにいないもの)を取りたいなーとふと思って調べたらあったので使ってみた!

EXCEPT句を使ってみた

SELECT
-- 0,2,4,6,8,10
generate_series( 0, 10, 2 )

EXCEPT

SELECT
-- 0,3,6,9
generate_series( 0, 10, 3 )

結果

2
4
10
8

※実際にはgenerate_seriesで使うなんてことはしませんw

まとめ

実際には、なかなか使いどころはないし、使わずに解決できることのほうが多いと思いますが、引き出しの一つとしては持っておいてよさそう。

(後MySQLにはこれに該当する関数がないんだってさ。。。)

10
4
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
10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?