如何在DB2中使用Search multiple LIKE for IN条件

kkih6yb8  于 2023-04-30  发布在  DB2
关注(0)|答案(4)|浏览(278)
SELECT *
  FROM XYZ
  WHERE column1 IN (X1,Y1,Z1);

我想在这个select查询中使用LIKE;如何用INLIKE语句,类似查询如下:

SELECT * 
  FROM XYZ 
  WHERE column1 LIKE IN  ($P{COLUMN});

另一个例子:-

select * from(select REPLACE(REFEV_VEH_TYPE,' ','')||REPLACE(REFEV_CATEGORY,' ','')||REPLACE(REFEV_USAGE,' ','')||将(REFEV_BODY_CODE,' ','')替换为TREF_ENF_VEHICLE_TYPE的UC)aa,其中aa.UC LIKE('%06TBISUM%')

UC
------
06TBISUM

select * from(select REPLACE(REFEV_VEH_TYPE,' ','')||REPLACE(REFEV_CATEGORY,' ','')||REPLACE(REFEV_USAGE,' ','')||将(REFEV_BODY_CODE,' ','')替换为TREF_ENF_VEHICLE_TYPE的UC)aa,其中aa.UC LIKE('% B')

UC
----------
06TGISJB
06TGITJB

select * from(select REPLACE(REFEV_VEH_TYPE,' ','')||REPLACE(REFEV_CATEGORY,' ','')||REPLACE(REFEV_USAGE,' ','')||将(REFEV_BODY_CODE,' ','')替换为TREF_ENF_VEHICLE_TYPE的UC)aa,其中aa.UC LIKE('% BAS')

UC
----------
06BCIBAS
05BCABAS
05BCBBAS

我想知道的结果是:

select * from(select REPLACE(REFEV_VEH_TYPE,' ','')||REPLACE(REFEV_CATEGORY,' ','')||REPLACE(REFEV_USAGE,' ','')||将(REFEV_BODY_CODE,' ','')替换为TREF_ENF_VEHICLE_TYPE的UC)aa,其中aa.UC IN LIKE('%06TBISUM%','% B','%BAS').

UC
------
06TBISUM
06TGISJB
06TGITJB
06BCIBAS
05BCABAS
05BCBBAS

我使用的参数是**$P{COLUMN}。我想使用IN LIKE($P {COLUMN})按$P {COLUMN}**参数搜索多个。有人知道吗?

roejwanj

roejwanj1#

LIKE运算在功能上等同于多个OR运算。所以...

SELECT *
  FROM XYZ
  WHERE ( column1 like 'X1' OR column1 like 'X2' OR column1 like 'X3' )
3df52oht

3df52oht2#

试试看
其中REGEXP_LIKE(aa.UC,+[06TBISUM.*|B|BAS].+')
将+替换为 *(如果我把 * 放在里面,它会翻译成斜体字)

chhkpiq4

chhkpiq43#

另一种简化的方法是像这样使用IN ('a','b','c')

select *
from tableA
where name IN ('Alberto','Chapote');

你可以在()里面放一些你想要的参数。

7vux5j2d

7vux5j2d4#

在DB2中不能将单个变量用作数组,但是可以使用递归CTE拆分它。
下面是一个不需要定义函数的版本:

WITH Split AS (SELECT '' AS value, txt, LOCATE(',', txt) AS nxt
               FROM (VALUES($P{COLUMN})) t(txt)
               UNION ALL
               SELECT DECODE(nxt, 0, txt, SUBSTR(txt, 1, nxt - 1)),
                      DECODE(nxt, 0, '', SUBSTR(txt, nxt + 1)),
                      DECODE(txt, '', 0, LOCATE(',', txt, nxt + 1))
               FROM Split
               WHERE LENGTH(txt) > 0), 

     SearchData AS (SELECT value
                    FROM Split
                    WHERE value <> '')

SELECT uc
FROM (SELECT REPLACE(refev_veh_type, ' ', '') 
              || REPLACE(refev_category, ' ', '') 
              || REPLACE(refev_usage, ' ', '') 
              || REPLACE(refev_body_code, ' ', '') AS uc
      FROM TRef_ENF_Vehicle_Type ) T
JOIN SearchData
  ON T.uc LIKE SearchData.value

(not测试,因为我手边没有DB2示例)
假设$P{COLUMN}包含以下字符串:'%06TBISUM%,%B,%BAS'

相关问题