sql语句
Posted MyAzhe0ci3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql语句相关的知识,希望对你有一定的参考价值。
drop PROCEDURE c
CREATE PROCEDURE c
()
BEGIN
DECLARE mobile VARCHAR(255);
DECLARE emergencyContactMobile VARCHAR(255);
DECLARE realName VARCHAR(255);
DECLARE identity VARCHAR(255);
DECLARE DoctorName VARCHAR(255);
DECLARE createdate VARCHAR(255);
DECLARE userLabelIds VARCHAR(255);
DECLARE userLabelName1 VARCHAR(255);
DECLARE userLabelName2 VARCHAR(255);
DECLARE userLabelName3 VARCHAR(255);
DECLARE userLabelName4 VARCHAR(255);
DECLARE userLabelName VARCHAR(255);
DECLARE dones INT;
DECLARE cur_user_id CURSOR FOR
SELECT
SUBSTRING(payload->"
.
m
o
b
i
l
e
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
p
a
y
l
o
a
d
−
>
"
.mobile",2,CHAR_LENGTH(payload->"
.mobile",2,CHARLENGTH(payload−>".mobile")-2) AS ‘mobile’,
SUBSTRING(payload->"
.
e
m
e
r
g
e
n
c
y
C
o
n
t
a
c
t
M
o
b
i
l
e
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
p
a
y
l
o
a
d
−
>
"
.emergencyContactMobile",2,CHAR_LENGTH(payload->"
.emergencyContactMobile",2,CHARLENGTH(payload−>".emergencyContactMobile")-2)AS ‘emergencyContactMobile’ ,
SUBSTRING(payload->"
.
r
e
a
l
N
a
m
e
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
p
a
y
l
o
a
d
−
>
"
.realName",2,CHAR_LENGTH(payload->"
.realName",2,CHARLENGTH(payload−>".realName")-2) AS ‘realName’ ,
SUBSTRING( payload->"
.
i
d
e
n
t
i
t
y
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
p
a
y
l
o
a
d
−
>
"
.identity",2,CHAR_LENGTH( payload->"
.identity",2,CHARLENGTH(payload−>".identity")-2) AS ‘identity’
,(SELECT real_name FROM blade_user
WHERE id=SUBSTRING(hr.payload->"
.
r
e
s
p
o
n
s
i
b
l
e
D
o
c
t
o
r
I
d
"
,
2
,
C
H
A
R
L
E
N
G
T
H
(
h
r
.
p
a
y
l
o
a
d
−
>
"
.responsibleDoctorId",2,CHAR_LENGTH(hr.payload->"
.responsibleDoctorId",2,CHARLENGTH(hr.payload−>".responsibleDoctorId")-2))AS ‘DoctorName’,
(SELECT create_time FROM hr_user_info WHERE id =hr.user_id ) AS ‘createdate’,
(SELECT user_label_ids FROM hr_user_info WHERE id =hr.user_id ) ‘userLabelIds’
FROM hr_f_personal_basic_information hr WHERE payload->"
.
m
o
b
i
l
e
"
=
"
无
"
A
N
D
p
a
y
l
o
a
d
−
>
"
.mobile"="无" AND payload->"
.mobile"="无"ANDpayload−>".emergencyContactMobile"=“无”;
#游标中的内容执行完后将done设置为1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET dones=1;
#打开游标
OPEN cur_user_id;
#执行循环
posLoop:LOOP
#判断是否结束循环
IF dones=1 THEN
LEAVE posLoop;
END IF;
#取游标中的值
FETCH cur_user_id INTO mobile,emergencyContactMobile,realName,identity,DoctorName,createdate,userLabelIds;
#截取人群标签id进行循环搜索然后拼接起来 (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(userLabelIds,’,’, 1), ‘,’, -1)) 通过逗号分割
SELECT short_name into userLabelName1 FROM hr_lable_info
WHERE id =(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(userLabelIds,’,’, 1), ‘,’, -1));
SELECT short_name into userLabelName2 FROM hr_lable_info
WHERE id =(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(userLabelIds,’,’, 2), ‘,’, -1));
SELECT short_name into userLabelName3 FROM hr_lable_info
WHERE id =(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(userLabelIds,’,’, 3), ‘,’, -1));
SELECT short_name into userLabelName4 FROM hr_lable_info
WHERE id =(SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(userLabelIds,’,’, 4), ‘,’, -1));
#select user_ids,mobil;
if userLabelName4 <=> userLabelName3 then
set userLabelName4="";
end if;
IF userLabelName3 <=> userLabelName2 then
set userLabelName3="";
end if;
IF userLabelName2 <=> userLabelName1 then
set userLabelName2="";
end if;
set userLabelName = CONCAT(userLabelName1,userLabelName2,userLabelName3,userLabelName4);
select userLabelName,mobile,emergencyContactMobile,realName,identity,DoctorName,createdate;
insert into new_table values(mobile,emergencyContactMobile,realName,identity,DoctorName,createdate,userLabelName);
END LOOP posLoop;
#释放游标
CLOSE cur_user_id;
END
SELECT * FROM new_table
call c();
以上是关于sql语句的主要内容,如果未能解决你的问题,请参考以下文章