Dev*_*erM 16 sql oracle sql-like
我知道我可以编写一个查询,它将返回给定列中包含任意数量值的所有行,如下所示:
Select * from tbl where my_col in (val1, val2, val3,... valn)
Run Code Online (Sandbox Code Playgroud)
但是,如果val1,例如,可以出现在任何地方my_col,其数据类型为varchar(300),我可能会写:
select * from tbl where my_col LIKE '%val1%'
Run Code Online (Sandbox Code Playgroud)
有没有办法梳理这两种技术.我需要搜索可能出现在列的自由格式文本中的任何位置的30个可能值.
以下列方式组合这两个语句似乎不起作用:
select * from tbl where my_col LIKE ('%val1%', '%val2%', 'val3%',....)
select * from tbl where my_col in ('%val1%', '%val2%', 'val3%',....)
Run Code Online (Sandbox Code Playgroud)
Luk*_*der 30
select * from tbl
where my_col like '%val1%' or my_col like'%val2%' or my_col like '%val3%', ...
Run Code Online (Sandbox Code Playgroud)
但要注意,这可能会很慢......或者,您可以将所有可接受的值(包括%符号)插入到表中并半连接该表:
select * from tbl
where exists (select 1 from all_likes where tbl.my_col like all_likes.value)
Run Code Online (Sandbox Code Playgroud)
对于真正的全文搜索,您可能希望查看Oracle Text:
http://www.oracle.com/technetwork/database/enterprise-edition/index-098492.html
AREGEXP_LIKE将进行不区分大小写的正则表达式搜索。
select * from Users where Regexp_Like (User_Name, 'karl|anders|leif','i')
Run Code Online (Sandbox Code Playgroud)
这将作为全表扫描执行- 就像LIKE or解决方案一样,因此如果表不小,性能将非常糟糕。如果它根本不经常使用,它可能没问题。
如果您需要某种性能,您将需要Oracle Text(或一些外部索引器)。
要使用 Oracle Text 获得子字符串索引,您需要一个 CONTEXT 索引。它有点复杂,因为它是为使用大量智能索引大型文档和文本而设计的。如果您有特殊需求,例如在数字和所有单词(包括“the”、“an”、“a”、空格等)中进行子字符串搜索,则需要创建自定义词法分析器来删除一些聪明的东西...
如果您插入大量数据,Oracle Text 不会加快速度,特别是如果您需要在事务内更新索引而不是定期更新。
是的,您可以使用此查询(而不是'Specialist'and 'Developer',键入您想要用逗号分隔的任何字符串并employees使用您的表更改表)
SELECT * FROM employees em
WHERE EXISTS (select 1 from table(sys.dbms_debug_vc2coll('Specialist', 'Developer')) mt where em.job like ('%' || mt.column_value || '%'));
Run Code Online (Sandbox Code Playgroud)
为什么我的查询比接受的答案更好:您不需要CREATE TABLE运行它的权限。这可以仅在SELECT权限下执行。
不,你不能这样做.IN子句中的值必须是完全匹配.你可以这样修改选择:
SELECT *
FROM tbl
WHERE my_col LIKE %val1%
OR my_col LIKE %val2%
OR my_col LIKE %val3%
...
Run Code Online (Sandbox Code Playgroud)
如果val1,val2,val3 ......足够相似,则可以在REGEXP_LIKE运算符中使用正则表达式.
小智 5
在 Oracle 中,您可以使用 regexp_like 如下:
select *
from table_name
where regexp_like (name, '^(value-1|value-2|value-3....)');
Run Code Online (Sandbox Code Playgroud)
插入符号 (^) 运算符指示行首字符 & 管道 (|) 运算符指示 OR 运算。
| 归档时间: |
|
| 查看次数: |
127392 次 |
| 最近记录: |