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.

postgresqlでXOR(排他的論理和)とNAND(否定論理積)の演算子を定義する

Last updated at Posted at 2015-05-01

postgresqlはリレーショナル・データベースなのにXORとNANDが使えない。
ので、オペレータを定義した。

sql(XOR)
CREATE FUNCTION xor(bool,bool) RETURNS bool AS '
SELECT ($1 AND NOT $2) OR (NOT $1 AND $2);
' LANGUAGE 'sql';
CREATE OPERATOR ~| (PROCEDURE='xor',LEFTARG=bool,RIGHTARG=bool);

http://snowland.net/nucleus/item/738 の丸パクリ。

sql(NAND)
CREATE FUNCTION nand(bool,bool) RETURNS bool AS '
SELECT NOT ($1 AND $2);
' LANGUAGE 'sql';
CREATE OPERATOR ^& (PROCEDURE='nand',LEFTARG=bool,RIGHTARG=bool);

こんな感じで使う。

sql(sample)
-- yoshikiとtoshiのいずれか
SELECT * FROM xjapan WHERE yoshiki ~| toshi;

--pataでない、かつhideでない
SELECT * FROM xjapan WHERE pata ~| hide;

追記
こちらの方法の方がスマートである。。
http://cs.hatenablog.jp/entry/2013/07/19/154215

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?