Det er ikke særlig klart, hvad du mener med er bestået , men du mener måske at $3
er en valgfri parameter med $3
fraværende betydning:ligeglad ?
SELECT *
FROM employee
WHERE name = $1
AND age = $2
AND ( $3 IS NULL OR salary = $3)
;
Nogle data:
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 )
;
Det samme som en forberedt forespørgsel:
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);
Output:
CREATE TABLE
INSERT 0 4
PREPARE
name | age | salary
-------+-----+--------
Alice | 13 | 3
(1 row)
name | age | salary
-------+-----+--------
Alice | 13 | 3
(1 row)