Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Oracle 12c-基于不同表中的值动态生成where子句_Sql_Oracle_Plsql_Dynamic Sql_Oracle12c - Fatal编程技术网

Sql Oracle 12c-基于不同表中的值动态生成where子句

Sql Oracle 12c-基于不同表中的值动态生成where子句,sql,oracle,plsql,dynamic-sql,oracle12c,Sql,Oracle,Plsql,Dynamic Sql,Oracle12c,我正在尝试编写一个Oracle 12c SQL语句,如下所示: select * from table1 where col1 in (*dynamicvalue*) 我希望动态值是以下SQL的结果: select col2 from table2 where rowid = 1 这可能吗?Col2包含如下值列表:“val1”、“val2”、“val3” 谢谢为什么不这样做呢 select t1.* from table1 t1 where t1.col1 in (select col2 f

我正在尝试编写一个Oracle 12c SQL语句,如下所示:

select * from table1 where col1 in (*dynamicvalue*)
我希望动态值是以下SQL的结果:

select col2 from table2 where rowid = 1
这可能吗?Col2包含如下值列表:“val1”、“val2”、“val3”

谢谢

为什么不这样做呢

select t1.*
from table1 t1
where t1.col1 in (select col2 from table2 where rowid = 1);
编辑:

将值列表存储在单个列中是一个非常糟糕的主意,以至于我错误地解释了这个问题。我将“值列表”作为存储在不同行中的值。为什么?因为这是存储数据的正确方法。在分隔列表中存储列列表不是SQLish

也就是说,我们有时会被其他人糟糕的设计决策所困扰。在这种情况下,可以使用如下查询:

select t1.*
from table1 t1
where exists (select 1
              from table2 t2
              where rowid = 1 and
                    ',' || t2.col2 || ',' like '%,' || t1.col1 || ',%'
             );

您可以拆分字符串并执行查询,而不是使用动态sql。要拆分字符串,请使用
regexp\u substr

select * from table1 
where col1 in (select regexp_substr(col2,'[^,]+', 1, level)  
               from table2
               connect by regexp_substr(col2, '[^,]+', 1, level) is not null)

第二个查询的结果不是列表。您应该查看
join
s。这里不需要动态sql。可能我没有正确地使用它,一条Col2记录一个值列表,如:'val1','val2','val3'可能与您的数据结构重复,因此列表不会存储在单个列中。谢谢!我认为这是一条正确的道路。有没有办法也使用动态SQL?我不知道用动态SQL怎么做。@AAA-你说的“也使用动态SQL”是什么意思?我恐怕你没有用“动态”这个词的既定的技术意义。您是否希望使用变量表名或列名?如果不是,您就不需要“动态”SQL;与其使用大词,不如用简单明了的英语来解释你想要什么。我相信OP的意思是
col2
是一个包含
'val1'、'val2'、'val3'
-的字符串,也就是说,他希望查询按照
SELECT*从表1执行,其中COL1位于('val1'、'val2'、'val3'))
-至少我是这样读的。YMMV.:-)@博比贾维斯。这很有道理。我将其解释为每行上的单个值。