Mysql 将不同的表处理为IF

Mysql 将不同的表处理为IF,mysql,stored-procedures,procedure,Mysql,Stored Procedures,Procedure,我想根据每个参数收到的值选择一个表。有没有可能像我的代码那样做呢?多谢各位 /*程序*/ BEGIN DECLARE text varchar(30); IF (type = 1) THEN SET text = 'table1'; ELSEIF (type = 2) THEN SET text = 'table2'; ELSEIF (type = 3) THEN SET text = 't

我想根据每个参数收到的值选择一个表。有没有可能像我的代码那样做呢?多谢各位

/*程序*/

BEGIN
    DECLARE text varchar(30);

    IF (type = 1) THEN  
        SET text = 'table1';
    ELSEIF (type  = 2) THEN
        SET text = 'table2';        
    ELSEIF (type  = 3) THEN
        SET text = 'table3';        
    ELSEIF (type  = 4) THEN
        SET text = 'table4';                
    END IF;

    Select * from text where `fee type` = var2;

END
BEGIN
    DECLARE text varchar(30);

    IF (type = 1) THEN  
        SET text = 'table1';
    ELSEIF (type  = 2) THEN
        SET text = 'table2';        
    ELSEIF (type  = 3) THEN
        SET text = 'table3';        
    ELSEIF (type  = 4) THEN
        SET text = 'table4';                
    END IF;

    SET @s = CONCAT('Select * from ',text,' where `fee type` = ?')
    PREPARE sentence FROM @s;
    SET @var1 = 2;
    EXECUTE sentence USING @var1;
    DEALLOCATE PREPARE sentence ;

END

查询与表的名称连接在一起,并在PREPARE中使用

/*程序*/

BEGIN
    DECLARE text varchar(30);

    IF (type = 1) THEN  
        SET text = 'table1';
    ELSEIF (type  = 2) THEN
        SET text = 'table2';        
    ELSEIF (type  = 3) THEN
        SET text = 'table3';        
    ELSEIF (type  = 4) THEN
        SET text = 'table4';                
    END IF;

    Select * from text where `fee type` = var2;

END
BEGIN
    DECLARE text varchar(30);

    IF (type = 1) THEN  
        SET text = 'table1';
    ELSEIF (type  = 2) THEN
        SET text = 'table2';        
    ELSEIF (type  = 3) THEN
        SET text = 'table3';        
    ELSEIF (type  = 4) THEN
        SET text = 'table4';                
    END IF;

    SET @s = CONCAT('Select * from ',text,' where `fee type` = ?')
    PREPARE sentence FROM @s;
    SET @var1 = 2;
    EXECUTE sentence USING @var1;
    DEALLOCATE PREPARE sentence ;

END

您尝试这样做的Google“mysql准备语句”强烈表明您的模式违反了规则。最好有一个单独的表,带有一个(索引的)列,其中包含一个指示符(例如字符串
'table1'
等),该指示符根据您的需要区分数据。