将不同的表处理成 IF

Procedure different tables into IF

我想select一个table根据每个参数收到的值。是否有可能像我的代码那样做?谢谢。

/*程序*/

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

查询与 table 的名称连接并在 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;

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

END