如何在存储过程MySQL中使用GROUP_CONCAT
How to use GROUP_CONCAT in Stored procedure MySQL
我正在尝试在存储过程中的另一个查询中使用逗号分隔值 (@job_skills),如下所示。但它没有按预期工作。艾米,它只考虑逗号分隔字符串中的第一个值。还有其他方法可以修补吗?
SELECT GROUP_CONCAT(skillId) as job_skills FROM tbljob_skill as jskl WHERE jskl.jobpostingId = param_jobid INTO @job_skills;
BLOCK6: BEGIN
DECLARE curl_job12 CURSOR FOR SELECT * FROM (SELECT js.jobseekerId FROM tbljobseeker_skill as jss INNER JOIN tbmstjobseeker as js ON (js.jobseekerId = jss.jobseekerId) INNER JOIN tblrecommended_jobseekers_details as rjd ON (js.jobseekerId=rjd.jobseekerId) WHERE jss.skillId IN (@job_skills) AND js.isActive = 'Y' AND js.lat<>0 AND js.lang<>0 AND rjd.sessionid=param_sessionid GROUP BY js.jobseekerId) as m;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET job_done12 = TRUE;
OPEN curl_job12;
read_loop_job12: LOOP
FETCH curl_job12 INTO jsid, miles;
IF job_done12 THEN
LEAVE read_loop_job12;
END IF;
INSERT INTO temp_skills (jobId,skill,jobseekerId) VALUES (param_sessionid,@job_skills,jsid);
IF jsid <> '' THEN
SELECT COUNT(1) INTO check_exists FROM tblrecommended_jobseekers_details WHERE sessionid = param_sessionid AND type = 'match 1 - SKILLS' AND jobseekerId = jsid;
IF check_exists = 0 THEN
START TRANSACTION;
INSERT INTO temp_jobseekers_scores (jobseekerId,score,type,miles) VALUES (jsid,score_skills,'match 1',miles);
INSERT INTO tblrecommended_jobseekers_details (jobseekerId,score,type,miles,sessionid) VALUES (jsid,score_skills,'match 1 - SKILLS',miles,param_sessionid);
COMMIT;
END IF;
END IF;
END LOOP;
CLOSE curl_job12;
END BLOCK6;
生成的逗号分隔值是一个 SET 并在与 IN
函数一起使用时被视为 单个 字符串。相反,您必须使用 FIND_IN_SET
来与其他值进行比较。
如下更改 CURSOR
定义中的 WHERE
子句。
DECLARE curl_job12 CURSOR FOR
SELECT * FROM (
SELECT js.jobseekerId
FROM tbljobseeker_skill as jss
INNER JOIN tbmstjobseeker as js
ON (js.jobseekerId = jss.jobseekerId)
INNER JOIN tblrecommended_jobseekers_details as rjd
ON (js.jobseekerId=rjd.jobseekerId)
-- WHERE jss.skillId IN (@job_skills)
WHERE FIND_IN_SET( jss.skillId, @job_skills )
AND js.isActive = 'Y'
AND js.lat<>0
AND js.lang<>0
AND rjd.sessionid=param_sessionid
GROUP BY js.jobseekerId
) as m;
参考文档:
- FIND_IN_SET(str,strlist)
- Returns 如果字符串 str 在
由 N 个子串组成的字符串列表 strlist
我正在尝试在存储过程中的另一个查询中使用逗号分隔值 (@job_skills),如下所示。但它没有按预期工作。艾米,它只考虑逗号分隔字符串中的第一个值。还有其他方法可以修补吗?
SELECT GROUP_CONCAT(skillId) as job_skills FROM tbljob_skill as jskl WHERE jskl.jobpostingId = param_jobid INTO @job_skills;
BLOCK6: BEGIN
DECLARE curl_job12 CURSOR FOR SELECT * FROM (SELECT js.jobseekerId FROM tbljobseeker_skill as jss INNER JOIN tbmstjobseeker as js ON (js.jobseekerId = jss.jobseekerId) INNER JOIN tblrecommended_jobseekers_details as rjd ON (js.jobseekerId=rjd.jobseekerId) WHERE jss.skillId IN (@job_skills) AND js.isActive = 'Y' AND js.lat<>0 AND js.lang<>0 AND rjd.sessionid=param_sessionid GROUP BY js.jobseekerId) as m;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET job_done12 = TRUE;
OPEN curl_job12;
read_loop_job12: LOOP
FETCH curl_job12 INTO jsid, miles;
IF job_done12 THEN
LEAVE read_loop_job12;
END IF;
INSERT INTO temp_skills (jobId,skill,jobseekerId) VALUES (param_sessionid,@job_skills,jsid);
IF jsid <> '' THEN
SELECT COUNT(1) INTO check_exists FROM tblrecommended_jobseekers_details WHERE sessionid = param_sessionid AND type = 'match 1 - SKILLS' AND jobseekerId = jsid;
IF check_exists = 0 THEN
START TRANSACTION;
INSERT INTO temp_jobseekers_scores (jobseekerId,score,type,miles) VALUES (jsid,score_skills,'match 1',miles);
INSERT INTO tblrecommended_jobseekers_details (jobseekerId,score,type,miles,sessionid) VALUES (jsid,score_skills,'match 1 - SKILLS',miles,param_sessionid);
COMMIT;
END IF;
END IF;
END LOOP;
CLOSE curl_job12;
END BLOCK6;
生成的逗号分隔值是一个 SET 并在与 IN
函数一起使用时被视为 单个 字符串。相反,您必须使用 FIND_IN_SET
来与其他值进行比较。
如下更改 CURSOR
定义中的 WHERE
子句。
DECLARE curl_job12 CURSOR FOR
SELECT * FROM (
SELECT js.jobseekerId
FROM tbljobseeker_skill as jss
INNER JOIN tbmstjobseeker as js
ON (js.jobseekerId = jss.jobseekerId)
INNER JOIN tblrecommended_jobseekers_details as rjd
ON (js.jobseekerId=rjd.jobseekerId)
-- WHERE jss.skillId IN (@job_skills)
WHERE FIND_IN_SET( jss.skillId, @job_skills )
AND js.isActive = 'Y'
AND js.lat<>0
AND js.lang<>0
AND rjd.sessionid=param_sessionid
GROUP BY js.jobseekerId
) as m;
参考文档:
- FIND_IN_SET(str,strlist)
- Returns 如果字符串 str 在 由 N 个子串组成的字符串列表 strlist