Oracle a value of a field to record a plurality of records split

Foreword

Before encountered such a demand, there was no record, this time they caught up, simply record it.

本文个人拙见,若有出入,请指出——来自菜的颤抖

Scenes

A table of information stored in the container, a container a record, a table B stored in the instruction operation for the container, an instruction includes a plurality of number of the container, separated by semicolons ;cut ( TCIU2347687;XUTR3546865), the current demand is for the instruction has been table B containers, when querying table a to be filtered out.

  • It is easy to think of not in, but separated by semicolons.
  • Secondly, not likehowever [Err] ORA-01427: 单行子查询返回多个行, it expressed likebehind only accept a single value fuzzy query.

It must be separated by semicolons recording, split into a separate record.
Single record
become:
After cutting

achieve

Oracle can be used regexp_substr函数to achieve, achieve cut above sql as:

select regexp_substr('TCIU2347687;XUTR3546865', '[^;]+', 1, level) JZXXH
from dual connect by level <= regexp_count('TCIU2347687;XUTR3546865', ';') + 1

In which regexp_substrthe parameters of meaning:

  • TCIU2347687;XUTR3546865 It represents the need to split the string matching (I am here just to make an example, under real circumstances be table columns).
  • [^;]+A typical regular expression, I cut a semicolon here, it is determined segmentation rules 多个不是分号的字符, therefore experiencing a semicolon will end, get a complete string.
  • 1Starting position, the extreme left ( the Oracle subscript is 1 start )
  • levelIt represents the first of several matches.
    In order to understand this point of view straight functions, such as the following statement:
select REGEXP_SUBSTR('aaa;bbb','[^;]+',1,1) AS STR FROM dual; 

The result is that aaa, if the second 2 becomes 1, is output bbb.
Well, this part of the intention is clear, here it is to take out each cutting string, see above take level个, and this levelis what is it, before this, look at the regexp_count(string, c)function, which is actually very good understanding, returns string the number of c.
Then comes the level, which is a pseudo-column, and RowNum similar,

SELECT LEVEL FROM DUAL CONNECT BY LEVEL <=2; 

level
So back to the original sql, also well understood.

At last

Sincerely, salute

Guess you like

Origin www.cnblogs.com/numen-fan/p/11365553.html