如果我想在一个参数中传递多个值,如何在存储过程中使用动态查询

时间:2017-08-16 13:30:56

标签: mysql

以下是示例存储过程:

在这里,我希望将模式传递为(1,2,(1,2))和user_types为(1,2或0,两者为(1,2))

我该如何写这种情况

    DELIMITER ;

    CREATE PROCEDURE `Statistics`
    (
       IN Start_Date DATE,
       IN END_Date DATE,
       IN Modes  int,
       IN User_Type INT
    )
    BEGIN

    -- User_Type = (0 0r 1 or 2)
    -- modes=(1 or 2 or (1,2))


    IF (User_Type=0)
    THEN
            select * from table 
            where modes in (1 or 2 or (1,2)) AND Dates BETWEEN Start_Date AND End_Date
       GROUP BY  Dates;
    ELSE
    IF(User_Type=1)
    THEN
              select * from table 
            where modes in (1 or 2 or (1,2)) AND Dates BETWEEN Start_Date AND End_Date

    ELSE
    IF (User_Type=2)
    THEN
                select * from table 
            where modes in (1 or 2 or (1,2)) AND Dates BETWEEN Start_Date AND End_Date

    END IF;

     END IF;
    END IF;
    END$$

    DELIMITER ; 

1 个答案:

答案 0 :(得分:0)

你必须使用准备好的声明!你可以在这里阅读它们:

http://www.mysqltutorial.org/mysql-prepared-statement.aspx