SQL 如何在sql中使用多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38419661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:43:52  来源:igfitidea点击:

How to use multiple values with like in sql

sqloracle

提问by Madhu Velayudhan

 select * from user_table where name in ('123%','test%','dummy%')

How to ensure that this whereclause is not an exact match, but a likecondition?

如何确保这个where子句不是完全匹配,而是一个like条件?

回答by trincot

In Oracle you can use regexp_likeas follows:

在 Oracle 中,您可以regexp_like按如下方式使用:

select *
from   user_table
where  regexp_like (name, '^(123|test|dummy)')

The caret (^) requires that the match is at the start of name, and the pipe |acts as an OR.

插入符号 ( ^) 要求匹配位于name的开头,管道|充当 OR。

Be careful though, because with regular expressions you almost certainly lose the benefit of an index that might exist on name.

不过要小心,因为使用正则表达式几乎肯定会失去name上可能存在的索引的好处。

回答by Mohammed Safeer

Use like this,

像这样使用,

select * 
from user_table 
where name LIKE '123%' 
OR name LIKE 'test%' 
OR name Like 'dummy%';

another option in MySQL

MySQL 中的另一种选择

select * from user_table where name REGEXP '^123|^test|^dummy';

回答by Husqvik

To not lose indexed access to rows in Oracle a table collection expression can be used:

为了不丢失对 Oracle 中行的索引访问,可以使用表集合表达式:

SELECT
    *
FROM
    user_table
    JOIN (SELECT column_value filter 
          FROM table(sys.odcivarchar2list('dummy%', '123%', 'test%'))
    ) ON user_table.name LIKE filter

The filter expressions must be distinct otherwise you get the same rows from user_tablemultiple times.

过滤器表达式必须不同,否则您会user_table多次获得相同的行。