如果参数为 'NOT PASSED' postgresql,如何避免 'where' 子句中的列
how to avoid column in 'where' clause if parameter is 'NOT PASSED' postgresql
运行 查询:
select * from employee where name = and age = and salary = ;
问题查询:
select * from employee where name = and age = ;
我怎样才能写出这样的逻辑
/* 如果 $3 被通过则 (and salary =
) */
注 :
/* 我不能 check For null or Empty as $3 如未通过,则 $3 将无法用于 check*/
在 Node 中我是这样使用的
postGresAdaptor.executeQueryWithParameters(QUERY, QUERYPARAMS, function(error, result) {
if (error) {
callback(error);
} else {
data = result.data;
}
});
不想在节点代码中添加逻辑,因为途中会有很多查询。
不太清楚你所说的是什么意思被传递,但也许你的意思是</code>是一个可选参数,<code>
没有意义: 不在乎 ?
SELECT *
FROM employee
WHERE name =
AND age =
AND ( IS NULL OR salary = )
;
一些数据:
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 =
AND age =
AND ( IS NULL OR salary = )
;
-- 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)
您可以使用 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;
运行 查询:
select * from employee where name = and age = and salary = ;
问题查询:
select * from employee where name = and age = ;
我怎样才能写出这样的逻辑
/* 如果 $3 被通过则 (and salary =
) */
注 : /* 我不能 check For null or Empty as $3 如未通过,则 $3 将无法用于 check*/
在 Node 中我是这样使用的
postGresAdaptor.executeQueryWithParameters(QUERY, QUERYPARAMS, function(error, result) {
if (error) {
callback(error);
} else {
data = result.data;
}
});
不想在节点代码中添加逻辑,因为途中会有很多查询。
不太清楚你所说的是什么意思被传递,但也许你的意思是</code>是一个可选参数,<code>
没有意义: 不在乎 ?
SELECT *
FROM employee
WHERE name =
AND age =
AND ( IS NULL OR salary = )
;
一些数据:
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 =
AND age =
AND ( IS NULL OR salary = )
;
-- 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)
您可以使用 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;