如何在sql中设置条件where?
我有一张我无法更改的桌子,它的结构如下 请求表如何在sql中设置条件where?,sql,oracle,Sql,Oracle,我有一张我无法更改的桌子,它的结构如下 请求表 ID PROCESS_ID STATUS DATE 1 1 -1 2002 2 2 1 2003 3 2 4 2004 4 3 5 2005 5 3 14 2005 6 1
ID PROCESS_ID STATUS DATE
1 1 -1 2002
2 2 1 2003
3 2 4 2004
4 3 5 2005
5 3 14 2005
6 1 15 2006
进程id是进程表的外键
那是
process_id process_name
1 a
2 b
3 c
我有一个输入参数:processID
我想要什么
if the paramter = 5
then return records that have STATUS 14 or 15 only
else
if
the paramter is null
then return all records
else if the paramter has any value except 5 , apply this condition
where :processID = requests_table.PROCESS_ID
如何做到这一点?
我无法向表中插入任何新记录您可以使用“执行即时查询” 定义一个varchar变量并将查询设置到其中,然后检查参数并设置查询的参数 看看这个例子:
declare
myQuery varchar(500) ;
parameter number := 5;
begin
myQuery := ' select * from requests_table ';
if parameter = 5 then
myQuery := myQuery || ' where (status=14 OR status=15) ' ;
end if;
EXECUTE IMMEDIATE myQuery;
end;
end;
参数是要传递给它的值您可以使用“执行即时查询” 定义一个varchar变量并将查询设置到其中,然后检查参数并设置查询的参数 看看这个例子:
declare
myQuery varchar(500) ;
parameter number := 5;
begin
myQuery := ' select * from requests_table ';
if parameter = 5 then
myQuery := myQuery || ' where (status=14 OR status=15) ' ;
end if;
EXECUTE IMMEDIATE myQuery;
end;
end;
参数是您希望传递给它的值,我建议使用调用SQL的任何东西(例如PHP)在不同的SQL语句之间切换,如
if(processID==5){
query="SELECT * FROM requests_table WHERE STATUS=14 OR STATUS=15"
} else if (processID==null){
query="SELECT * FROM requests_table"
} else {
query="SELECT * FROM requests_table WHERE PROCESS_ID=processID"
}
然后运行所选的查询
如果您想在一条SQL语句中实现这一切,您可以这样做
SELECT * FROM requests_table WHERE (processID=5 AND (STATUS=14 OR STATUS=15)) OR processID NULL OR PROCESS_ID=processID
我建议使用调用SQL的任何东西(例如PHP)在不同的SQL语句之间切换,如
if(processID==5){
query="SELECT * FROM requests_table WHERE STATUS=14 OR STATUS=15"
} else if (processID==null){
query="SELECT * FROM requests_table"
} else {
query="SELECT * FROM requests_table WHERE PROCESS_ID=processID"
}
然后运行所选的查询
如果您想在一条SQL语句中实现这一切,您可以这样做
SELECT * FROM requests_table WHERE (processID=5 AND (STATUS=14 OR STATUS=15)) OR processID NULL OR PROCESS_ID=processID
您可以在单个语句中执行此操作,如下所示:
SELECT *
FROM requests_table
WHERE (:processID = 5 AND STATUS IN (14, 15)) OR
(:processID = processId) OR
(:processID IS NULL);
您可以在单个语句中执行此操作,如下所示:
SELECT *
FROM requests_table
WHERE (:processID = 5 AND STATUS IN (14, 15)) OR
(:processID = processId) OR
(:processID IS NULL);
其中(参数=5和状态在(14,15)中)或(参数5和:进程ID=请求表。进程ID)也可参见其中(参数=5和状态在(14,15)中)或(参数5和:进程ID=请求表。进程ID)也可参见