合格の意味が明確ではありません 、しかし多分あなたはそれを意味します$3
はオプションのパラメータで、$3
意味がない:気にしない ?
SELECT *
FROM employee
WHERE name = $1
AND age = $2
AND ( $3 IS NULL OR salary = $3)
;
一部のデータ:
CREATE TABLE employee
( name varchar NOT NULL PRIMARY KEY
, age integer
, salary integer
);
INSERT INTO employee ( name , age , salary ) VALUES
( 'Alice' , 13 , 3 )
,( 'Bob' , 11 , 5 )
,( 'Charlotte' , 15 , 9 )
,( 'David' , 17 , 10 )
;
準備されたクエリと同じ:
PREPARE qry (text, integer , integer) AS
SELECT *
FROM employee
WHERE name = $1
AND age = $2
AND ( $3 IS NULL OR salary = $3)
;
-- and call the prepared statement:
EXECUTE qry ('Alice', 13, 3);
EXECUTE qry ('Alice', 13, NULL);
出力:
CREATE TABLE
INSERT 0 4
PREPARE
name | age | salary
-------+-----+--------
Alice | 13 | 3
(1 row)
name | age | salary
-------+-----+--------
Alice | 13 | 3
(1 row)