如何编写SQL查询以从表中选择行,其中where子句中的列将根据发送的内容具有不同的值。 示例:发送的参数对于单个列名称具有3个不同的值。 我如何检索满足所有参数的行。
答案 0 :(得分:0)
您可以使用IN / NOT IN构造...
匹配参数中具有列值的所有列:
SELECT * FROM table WHERE `columnName` IN ('parameter1','parameter2','parameter3');
要匹配参数中没有列值的所有列:
SELECT * FROM table WHERE `columnName` NOT IN ('parameter1','parameter2','parameter3');
答案 1 :(得分:0)
我认为你有很多不确定的潜在价值。
在这种情况下创建一个函数:
CREATE FUNCTION [dbo].[fnSplit](
@sInputList VARCHAR(max) -- List of delimited items
, @sDelimiter VARCHAR(5) -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(max))
BEGIN
DECLARE @sItem VARCHAR(max)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
您现在可以按以下方式编写查询:
SELECT * FROM myTable WHERE myColumn IN
(SELECT * FROM dbo.fnSplit(stringParamHere, stringSeparatorHere))
通过这种方式,您可以使用给定的分隔符在单个参数中传递x值列表。
以上示例用于分隔字符串值。然而,只需更改表项的类型并将“INSERT INTO ...”行更改为:
,就可以非常轻松地生成返回整数表的fnSplitInt。INSERT INTO @List SELECT CAST(@sItem as int)
postgresql的相同功能如下:
CREATE OR REPLACE FUNCTION public.fnsplit(
IN stringlist character varying,
IN delimit character varying)
RETURNS TABLE(items character varying) AS
$BODY$
declare remainderlist character varying;
declare front character varying;
declare delimitpos integer;
begin
drop table if exists tmptbl;
create temp table tmptbl(items character varying);
remainderlist := $1;
delimitpos := strpos(remainderlist, $2);
while delimitpos > 0 loop
front := trim(both from(left(remainderlist, delimitpos -1)));
remainderlist := substr(remainderlist, delimitpos + 1);
if length(front) > 0 then
insert into tmptbl values (front);
end if;
delimitpos := strpos(remainderlist, $2);
end loop;
--insert last value
remainderlist := trim(both from remainderlist);
if length(remainderlist) > 0 then
insert into tmptbl values (remainderlist);
end if;
return query
select * from tmptbl;
return;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000;
HTH
答案 2 :(得分:0)
尝试此选项以匹配大量值
SELECT * FROM table WHERE columnName IN (select
unnest(regexp_split_to_array(comma_separated_values::text, ',')));
或者你也可以用Function
作为输入参数写一个comma_separated_values
,如
CREATE OR REPLACE FUNCTION myFunction(comma_separated_values Text)
RETURNS refcursor AS
$BODY$
DECLARE
cur_data refcursor;
begin
open cur_data for
SELECT * FROM table WHERE columnName IN (select
unnest(regexp_split_to_array(comma_separated_values::text, ',')));
return cur_data ;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
编辑:您可以使用
text array
创建另一个函数input
参数
CREATE OR REPLACE FUNCTION myFunction(values Text[])
RETURNS refcursor AS
$BODY$
DECLARE
cur_data refcursor;
begin
open cur_data for
SELECT * FROM table WHERE columnName IN (select unnest(values));
return cur_data ;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
希望它有所帮助。