关于Oracle中instr和wm_concat函数的使用

先建立两张表

instr是字符查找函数,如果找到会返回第一次出现的位置,我们可以用作模糊查询,例如:

select * from table2 where instr(table1Id, '1') > 0

结果为:

可以看到查询出来的是table1Id列包括1的字符串。

我们可以通过table2中的table1Id去查找table1的名字,例如我们查询table2中id= 2的数据包含table1中的哪些名字:

select * from table1 t where instr(';' || (select table1Id from table2 where id = 1) || ';', ';' || t.id || ';') > 0;

结果为:

table2中id=1的table1Id为"2;3",所以查询出来的是table1中id为2和3的数据。

关于wm_concat函数,该函数可以把列值以","号分隔起来,并显示成一行。例如:

select wm_concat(t.name) from table1 t

可是结果发现为plsql中为<clob>,只需要再使用to_char函数就可以了

select to_char(wm_concat(t.name)) from table1 t

结果为:

我们再以上面select * from table1 t where instr(';' || (select table1Id from table2 where id = 1) || ';', ';' || t.id || ';') > 0;这条语句为例将查询结果一行输出:

select to_char(wm_concat(t.name)) from table1 t where instr(';' || (select table1Id from table2 where id = 1) || ';', ';' || t.id || ';') > 0;

结果为:

猜你喜欢

转载自blog.csdn.net/u010689849/article/details/82021170