sql field splitting, even get the value table subquery

Table 1. Even sub-query to get value

select bas.name,bas.id_card_num,bas.mobil_no,gender,bas.birthday,bas.height,bas.weight,province.value as province,city.value as city,area.value as area,profession.value as profession from xy_user_baseinf bas
LEFT JOIN
(select usr_no,`value`FROM `xy_user_otherInf` where fid='99994' and id='1')province
ON bas.usr_no=province.usr_no
left join
(select usr_no,`value`FROM `xy_user_otherInf` where fid='99994' and id='2')city
on bas.usr_no=city.usr_no
left join
(select usr_no,`value`FROM `xy_user_otherInf` where fid='99994' and id='3')area
on bas.usr_no=area.usr_no
left join
(select usr_no,`value`FROM `xy_user_otherInf` where fid='99992' and id='1')profession
on bas.usr_no= profession.usr_no
where bas.id_card_num=#{id_card_num}

2. Query Split

 

Splitting the string delimiters


SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
FROM
mysql.help_topic
WHERE
help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1

A string split: SUBSTRING_INDEX (str, delim, count)

Illustrates parameters
Parameter Name Dictionary
str need to split string
delim delimiter, split by a character
count when the count is positive, all of the characters before the n-th separator taken; if count is negative, taking the reciprocal of the n-th All characters following the delimiter.
2. Example
(1) to get the first two "," all characters until a comma delimited.

SUBSTRING_INDEX('7654,7698,7782,7788',',',2)

2) to obtain the penultimate 2 "," all the characters after the comma delimited

SUBSTRING_INDEX('7654,7698,7782,7788',',',-2)

Second, the replacement function: replace (str, from_str, to_str)

Illustrates parameters
Parameter Name Dictionary
string str require replacement
from_str string needs to be replaced
to_str need to replace the string
2. Example
(1) The separator "," comma replaced by "" empty.

REPLACE('7654,7698,7782,7788',',','')
1


Third, access to the string length: LENGTH (str)

Illustrates parameters
Parameter Name Dictionary
str need to calculate the length of the string
2. Example
(1) Get the length of the string '7654,7698,7782,7788'

LENGTH('7654,7698,7782,7788')
1


SQL parsing implementation

SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1) AS num
FROM
mysql.help_topic
WHERE
help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1


Here help_topic_id help_topic use mysql database table as a variable because help_topic_id increment is, of course, you can also use auto-increment fields other auxiliary table.

help_topic table:


Implementation steps:
Step1: the last number of acquired first split into strings required by the analog to traverse the n help_topic_id string.

According to the code fragment:

help_topic_id < LENGTH('7654,7698,7782,7788')-LENGTH(REPLACE('7654,7698,7782,7788',',',''))+1
1
2


Step2: According to "," Comma to split string, here using SUBSTRING_INDEX (str, delim, count) function, assign the result to the final num fields.

According to the code fragment:

SUBSTRING_INDEX (SUBSTRING_INDEX ( '7654,7698,7782,7788', ',', help_topic_id +. 1), ',', -. 1) the AS NUM
. 1
Step:
In "," Comma Separated value according to the help_topic_id to intercept all the n + 1 before the string delimiters. (Where n + 1 is counted from the beginning because help_topic_id is 0, where the need to begin obtaining from a delimiter.)

SUBSTRING_INDEX ( '7654,7698,7782,7788', ',', help_topic_id +. 1)
. 1
EG:
When help_topic_id = 0, the character string acquired = 7654
when help_topic_id = 1, the character string acquired = 7654, 7698
... (and so on)

Step Two:
In "," Comma Separated Values, all strings taken after a penultimate separators.

SUBSTRING_INDEX (SUBSTRING_INDEX ( '7654,7698,7782,7788', ',', help_topic_id +. 1), ',', -. 1)
. 1
EG:
The first step, when help_topic_id = 0, the character string acquired = 7654, then the second step taken string = 7654
in accordance with the first step, when help_topic_id = 1, the obtained string = 7654,7698, then the second step taken string = 7698
... (so )

Guess you like

Origin www.cnblogs.com/xianz666/p/12168245.html