简体   繁体   English

DB2 具有多个值的 like 子句

[英]DB2 like clause with multiple values

How to use like clause with multiple values in DB2 version 12.01.如何在 DB2 版本 12.01 中使用具有多个值的 like 子句。

SELECT t1.* FROM table1 t1, ( select (pattern_col) as term from help_table ) t2 WHERE t1.col1 like t2.term SELECT t1.* FROM table1 t1, ( select (pattern_col) as term from help_table ) t2 WHERE t1.col1 like t2.term

Pattern_col contains n number of values like(%abc%, %xyz%, %nnn%, ...) Pattern_col 包含 n 个值,例如 (%abc%, %xyz%, %nnn%, ...)

Thanks in advance for your time and help.预先感谢您的时间和帮助。

I tried this solution mentioned in How to use LIKE with IN in DB2?我尝试了How to use LIKE with IN in DB2? 中提到的这个解决方案? . . It works when I use sysdummy table (Oracle equivalent is DUAL)它在我使用 sysdummy 表时有效(Oracle 等效项是 DUAL)

But when i try to replace the sysdummy1 with actual table values, i get below error.但是当我尝试用实际表值替换 sysdummy1 时,出现以下错误。 SQLCODE = -132, ERROR: AN OPERAND OF LIKE IS NOT VALID SQLCODE = -132,错误:类似的操作数无效

I am not able to figure out why it works with sysdummy1 and why not with actual table.我无法弄清楚为什么它适用于 sysdummy1 以及为什么不适用于实际表。

There is nothing wrong with your approach (I guess), but the platform/version you are on may be a problem.您的方法没有任何问题(我猜),但是您所在的平台/版本可能有问题。 Example for Db2 11.5 on LUW: LUW 上 Db2 11.5 的示例:

create table patterns (pid int not null primary key, pattern varchar(100) not null);
insert into patterns (pid, pattern) values (1, '%abc% %xyz% %nnn%');

create table t (val varchar(100) not null primary key);
insert into t (val) values ('xyzabc xyz nnn'),('xyzabc xyz nn');

select t.*
from t
join patterns p
    on t.val like p.pattern
where p.pid = 1;

VAL                                                                                                 
----------------------------------------------------------------------------------------------------
xyzabc xyz nnn

You can insert multiple patterns like:您可以插入多个模式,例如:

delete from patterns;
insert into patterns (pid, pattern) 
values (1, '%abc%'), (2, '%xyz%'), (3, '%nnn%');

select t.*
from t
join patterns p
    on t.val like p.pattern
where p.pid = 1;

VAL                                                                                                 
----------------------------------------------------------------------------------------------------
xyzabc xyz nn                                                                                       
xyzabc xyz nnn

From your comment it appears as if you are using zos.从您的评论看来,您似乎在使用 zos。 Both LUW and ZOS have regexp abilities that you may want to explore: LUW 和 ZOS 都具有您可能想要探索的正则表达式功能:

REGEXP_LIKE 正则表达式_LIKE

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

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