我想根据每个参数接收的值选择一个表。是否有可能与我的代码类似?谢谢。
/ * 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 = 'table3';
ELSEIF (type = 4) THEN
SET text = 'table4';
END IF;
Select * from text where `fee type` = var2;
END
答案 0 :(得分:1)
查询与表的名称连接并在PREPARE中使用。
/ * 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 = '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