gpt4 book ai didi

mysql - 将不同的表处理成 IF

转载 作者:行者123 更新时间:2023-11-29 07:38:31 25 4
gpt4 key购买 nike

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

/*过程*/

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

最佳答案

查询与表名连接并在 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

关于mysql - 将不同的表处理成 IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47591276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com