如何在存储过程MySQL中使用GROUP_CONCAT

时间:2015-07-14 10:33:43

标签: mysql stored-procedures nested-loops group-concat

我试图在另一个查询中使用逗号分隔值(@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;

1 个答案:

答案 0 :(得分:0)

生成的逗号分隔值是 SET ,与IN函数一起使用时将其视为字符串。相反,您必须使用FIND_IN_SET与其他值进行比较。

更改WHERE定义中的CURSOR条款,如下所示。

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)
    • 如果字符串str在,则返回1到N范围内的值 字符串列表strlist由N个子字符串组成