运行查询:
select * from employee where name = $1 and age = $2 and salary = $3;
问题查询:
select * from employee where name = $1 and age = $2;
如何编写的逻辑
/ *如果$ 3被通过则(and salary = $3
)* /
注意: / *我无法检查 null 或清空为 $ 3 未通过 $ 3 check * /
将无法使用在Node I中使用Like This
postGresAdaptor.executeQueryWithParameters(QUERY, QUERYPARAMS, function(error, result) {
if (error) {
callback(error);
} else {
data = result.data;
}
});
不想在节点代码中添加逻辑,因为路上会有很多查询。
答案 0 :(得分:0)
传递的含义不是很清楚,但也许你的意思是FinalWrapper
是一个可选参数,instance
缺席含义: don “关心?
$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 )
;
答案 1 :(得分:0)
您可以使用default参数类型:
CREATE OR REPLACE FUNCTION _my_function(_name character varying, _age integer, _salary integer default NULL)
RETURNS character varying AS
$BODY$
begin
if (_salary IS NULL) then
select * from employee where name = _name and age = _age ;
else
select * from employee where name = _name and age = _age and salary = _salary;
end IF;
return 'OK';
end;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;