在列中显示 LIKE 条件匹配项

Show LIKE condition matches in a column

我有一个类似的查询:

SELECT fname, lname
FROM names
WHERE gcode LIKE %5536% OR fname LIKE %3663%

这样的条件有50个,条款有点长,但大意是这样的。我们无法制作临时表,所以这就是我们要走的路线。

我试图 return 输出在其自己的列中匹配的类似条件(即,%5536% 在其自己的列中 returned)

我可以通过编程方式突出显示它,但是在 SQL 中有没有办法做到这一点?

您可以在 SQL 中执行此操作。这是一种方法:

select n.*
from (select n.*,
             ((case when gcode LIKE %5536%  then '5536;' end) ||
              (case when gcode LIKE %3663%  then '3663;' end) ||
              . . .
             ) as matches
      from names
     ) n
where matches is not null;

您可以将匹配条件存储在一个集合中:

SELECT n.fname,
       n.lname,
       t.COLUMN_VALUE AS match
FROM   name n
       INNER JOIN
       TABLE( SYS.ODCIVARCHAR2LIST( '%5536%', '%3663%' ) ) t
       ON ( n.gcode LIKE t.COLUMN_VALUE );