简体   繁体   English

Oracle在IN条件内使用字符串

[英]Oracle use a string inside IN condition

I have a select statement where I use the IN condition, and inside the IN condition I have a string...how can I ignore the single quotes? 我有一个使用IN条件的select语句,在IN条件中有一个字符串...如何忽略单引号?

Select * from employ where id = 12 and org_id in ({$id})
$id = '12,13'

Thanks 谢谢

in is used with collections , so your input string first should be converted into collection (by breaking into rows, based on comma delimiter) in是与collections ,因此首先应将您的输入字符串转换为collection (基于逗号分隔符,分成几行)

Try something like this 试试这个

Select * from employ where id = 12 and org_id in (
SELECT decode(:input_id,null,  (select  employ.org_id from dual) 
,TRIM(REGEXP_SUBSTR(temp, '[^,]+', 1, level)) )  
    FROM (SELECT  :input_id temp FROM DUAL)
    CONNECT BY level <= REGEXP_COUNT(temp, '[^,]+')
    )

by the way, this org_id in () will return true if :input_id is null . 顺便说一句,如果:input_idnull org_id in ()将返回true

Another approach would be to construct the whole query as a string first and then execute it either with execute immediate or through php . 另一种方法是首先将整个查询构造为一个字符串,然后使用execute immediate或通过php执行它。 However this could raise sql injection concerns. 但是,这可能会引起sql注入问题。

I am guessing that you want to process a list. 我猜您想处理一个列表。 Unfortunately, SQL doesn't allow a parameter to be a list, so you have to do more work. 不幸的是,SQL不允许将参数作为列表,因此您必须做更多的工作。

One method uses like (or a regular expression): 一种方法使用like (或正则表达式):

Select *
from employ
where id = 12 and
      ',' || org_id || ',' like '%,' || {$id} || ',%';

If performance is an issue and you want to use indexes, you may need to investigate other options. 如果性能是一个问题,并且您想使用索引,则可能需要研究其他选项。 The simplest is to dispense with the parameter and just modify the query string (using dynamic SQL to execute the query). 最简单的方法是省去参数,而只需修改查询字符串(使用动态SQL执行查询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM