[英]Select From a Table where field in All fields
I have to select values from a table in all rows like this: 我必须从像这样的所有行的表中选择值:
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (select SCHM_CODE
from DLY_DEP_VIEW )
group by schm_code
I will be taking input from user input, I do not want the select statement in the brackets, I need to return a value for all in there like: 我将从用户输入中获取输入,我不希望括号中的select语句,我需要为其中的所有内容返回一个值:
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (ALL_SCHM_CODES)
group by schm_code
And this is Giving me Invalid Identifier: (EDITS) 这是给我无效的标识符:(编辑)
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (select regexp_substr('" + c + "', '[^,]+',1,level) p
from dual t
connect by level <= regexp_count('" + c + "', ',') + 1
)
group by schm_code;
Since the value in the brackets keep changing in my application. 由于括号中的值在我的应用程序中不断变化。 What is the best way to achieve this? 实现此目标的最佳方法是什么? The query is inside Java Code. 该查询在Java代码内部。
You can try something like this: 您可以尝试如下操作:
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (select regexp_substr(:your_string, '[^,]+',1,level) p
from dual t
connect by level <= regexp_count(:your_string, ',') + 1
)
group by schm_code
:your_string
is the string you got as input from the user which can contain one value or many (comma separated) :your_string
是您从用户输入获得的字符串,可以包含一个或多个值(逗号分隔)
Here is a sqlfiddle demo 这是一个sqlfiddle演示
BTW, use a prepared statement with a bind variable, don't just concatenate the input string. 顺便说一句,请使用带有绑定变量的预处理语句,而不仅仅是连接输入字符串。
Read more here 在这里阅读更多
You can use a nested table, as one of the methods: 您可以使用嵌套表作为以下方法之一:
Create a nested table type. 创建一个嵌套表类型。 Assumption was made that the Schm_code
is of number datatype. 假定Schm_code
是数字数据类型。
SQL> create or replace type t_list as table of number 2 / Type created
Rewrite the query as follows. 如下重写查询。 If a list is a list of strings, then each element of the list must be enclosed with single quotation marks: 如果列表是字符串列表,则列表的每个元素都必须用单引号引起来:
select distinct SCHM_CODE, sum(DEP_AMT) as AMOUNT from DLY_DEP_VIEW where Schm_code in (Select column_value from table(t_list(<<your list of codes>>))) group by schm_code
In this example, for the sake of demonstration, Sql*plus has been used to execute a query and elements has been typed manually : 在此示例中,为了演示起见,使用Sql * plus执行查询,并手动键入了元素:
SQL> select first_name
2 , last_name
3 from employees t
4 where t.employee_id in (select column_value
5 from table(t_list(&list))
6 );
Enter value for list: 100,200
old 5: from table(t_list(&list))
new 5: from table(t_list(100,200))
FIRST_NAME LAST_NAME
-------------------- -------------------------
111 King
Jennifer Whalen
SQL> select first_name
2 , last_name
3 from employees t
4 where t.employee_id in (select column_value
5 from table(t_list(&list))
6 );
Enter value for list: 110,300,220
old 5: from table(t_list(&list))
new 5: from table(t_list(110,300,220))
FIRST_NAME LAST_NAME
-------------------- -------------------------
John Chen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.