Mysql多个参数,在存储过程中有多个查询

时间:2014-09-22 03:18:14

标签: mysql sql stored-procedures

我需要创建一个' select' mysql程序将接受多个参数,内联此程序将选择其他表

目标

  1. 存储过程必须接受多个参数
  2. 使用多个参数,程序应选择table_a,table_b和table_c
  3. 目前我正在使用此代码(它接受多个参数,但我不知道如何修改它以便在table_b和table_c上执行另一个选择)

    DELIMITER //
    
    CREATE PROCEDURE select_multiple_object(IN user_ids VARCHAR(65535))
    BEGIN
        SET @query = CONCAT ('SELECt * FROM table_a WHERE userid IN (',user_ids,')');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END //
    DELIMITER ;
    

    我真正想要实现的是:

    DELIMITER //
    
    CREATE PROCEDURE select_multiple_object(IN user_ids VARCHAR(65535))
    BEGIN
        SET @query = CONCAT ('
            SELECt * FROM table_a WHERE userid IN (',user_ids,');
            SELECt * FROM table_b WHERE userid IN (',user_ids,');
            SELECt * FROM table_c WHERE userid IN (',user_ids,');
        ');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END //
    DELIMITER ;
    
    
    
    CALL select_multiple_object('1,2,3')
    

1 个答案:

答案 0 :(得分:0)

首先汇编MySQL查询,然后发送查询,类似于以下内容:

$parameter = "user_ids";
$table = "table_a";
$query = 'SELECT * FROM ' . $parameter . ' WHERE userid IN (",' . $parameter . ',")';
$result = mysql_query($query);

要输入多个参数,只需使用循环根据需要重复此过程。