存储过程中的MySQL select语句返回的结果与proc之外的结果不同

时间:2016-02-22 20:37:48

标签: mysql select stored-procedures resultset select-into

我无法弄清楚为什么在mysql proc之外运行的完全相同的查询不会在其中返回相同的数据。

这是proc。

DELIMITER $$

USE `poll_app`$$

DROP PROCEDURE IF EXISTS `record_new_vote`$$

CREATE DEFINER=`somemysqluser`@`%` PROCEDURE `record_new_vote`(
    p_member_id INT (10),
    p_option_ids VARCHAR(255), -- 12,13,14,15
    p_option_id INT (10),
    p_stream_id INT (10),
    p_scene_id INT (10)
    )
BEGIN

    DECLARE _user_already_voted VARCHAR(255);
    DECLARE _update_off_id INT;
    DECLARE _update_on_id INT;

    -- 1. Query to figure out what to update

        SELECT
            p_member_id, p_option_ids, p_option_id, p_stream_id, p_scene_id;

        SET SESSION group_concat_max_len = 1000000;

        SELECT
            GROUP_CONCAT(selections.status, "~~~", selections.id, "~~~", selections.option_id),
            GROUP_CONCAT(IF(selections.status = "ON", id, NULL)),
            GROUP_CONCAT(IF(selections.status = "OFF" && selections.option_id = p_option_id, selections.id, NULL))
        INTO
            _user_already_voted, _update_off_id, _update_on_id
        FROM
            selections
        WHERE
            selections.member_id = p_member_id
        AND
            selections.option_id IN (p_option_ids)
        AND
            selections.stream_id = p_stream_id
        AND
            selections.scene_id = p_scene_id;

    -- 2. Check to see if the user has already voted on the poll or not


        IF (_user_already_voted IS NOT NULL) THEN

            SELECT
                _user_already_voted, _update_off_id, _update_on_id;         
        END IF;
    END$$

DELIMITER ;

这是输出的内容

[ [ RowDataPacket {
      p_member_id: 107,
      p_option_ids: '1005,1006,1007,1008',
      p_option_id: 1007,
      p_stream_id: 7,
      p_scene_id: 1 } ],
  [ RowDataPacket {
      _user_already_voted: 'OFF~~~451~~~1005',
      _update_off_id: NULL,
      _update_on_id: NULL } ],

当我在proc之外运行select查询时(插入了相同的数据)

    SELECT
        GROUP_CONCAT(`status`, "~~~", id, "~~~", option_id) AS "selections",
        GROUP_CONCAT(IF(`status` = "ON", id, NULL)) AS "id_to_turn_status_off",
        GROUP_CONCAT(IF(`status` = "OFF" && option_id = 1007, id, NULL)) AS "id_to_turn_status_on"
    FROM
        selections
    WHERE
        member_id = 107
    AND
        option_id IN (1005,1006,1007,1008)
    AND
        stream_id = 7
    AND
        scene_id = 1

结果

selections                          id_to_turn_status_off        id_to_turnstatus_on
 OFF~~~451~~~1005,ON~~~452~~~1006            452                      null

如您所见,组concat字符串不长,id_to_turn_status_off为452,而同一查询在proc中运行时变量_update_off_id为NULL。

我无法想象我的生活,任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

我发现了伙计们。

显然

IN("1005,1006,1007,1008") 

不等同于

IN(1005,1006,1007,1008)

在我在proc中的查询中,因为它接受的参数是一个字符串,它使用双引号版本" 1005,1006,1007,1008",其中我在外面运行的查询是非双引号版本1005,1006,1007,1008

这就是导致差异的原因。

我通过使用此更正

来纠正它
FIND_IN_SET(selections.option_id, p_option_ids)

或在查询之外

FIND_IN_SET(selections.option_id, "1005,1006,1007,1008")

答案 1 :(得分:0)

很高兴你明白了:)

它似乎部分工作的原因是因为类型转换。

p_option_ids VARCHAR(255)
p_option_id INT (10),

selections.option_id IN (p_option_ids)

这是试图在VARCHAR中找到一个INT。 MySQL将字符串动态地转换为int,因此它会向下移动字符串,直到找到非数字字符并截断其余字符。 “1005,1006,1007,1008”转换为1005(截断“,1006,1007,1008”)。这就是它找到option_id = 1005行而不是option_id = 1007行的原因。