gpt4 book ai didi

MySQL 从变量中选择列名

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

我有一个触发器,它将 case 语句的值存储在变量 @SHIFTIME 中,并且通过返回值 Shift1、Shift2 或 Shift3 可以正常工作。我现在需要在引用列名称的 select 语句中使用此变量的结果。我尝试将其存储在变量@SHIFTSELECT中:

  SET @SHIFTSELECT := (SELECT @SHIFTIME FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME); 

所以 @SHIFTIME 是列名,我希望 @SHIFTSELECT 将此解释为:

SELECT `Shift1` FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME

但问题是 @SHIFTSELECT 返回与 @SHIFTIME 相同的值,即列名(Shift1、Shift2 或 Shift3),而不是所需的 ROW 结果。这是我的完整触发器:

CREATE TRIGGER `oee_upd_final` AFTER INSERT ON `oee_main_interim`
FOR EACH ROW BEGIN
DECLARE SHIFTIME TEXT;
DECLARE SHIFTSELECT TEXT;
SET @SHIFTIME := ( Select

(Case
When ((CurTime() > oee_machinenames.Shift1) And
(CurTime() < oee_machinenames.Shift2)) Then 'Shift1'
When ((CurTime() > oee_machinenames.Shift2) And
(CurTime() < oee_machinenames.Shift3)) Then 'Shift2'
When ((CurTime() > oee_machinenames.Shift3) Or
(CurTime() < oee_machinenames.Shift1)) Then 'Shift3' End)
From
oee_machinenames
where
oee_machinenames.ID = New.NAME

Group By
oee_machinenames.ID);

SET @SHIFTSELECT := (SELECT @SHIFTIME FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME);


INSERT INTO `oee_main_interim_final` (id,NAME,ts,Left_IO,Left_NIO,Recovery,Right_IO,
Right_NIO,RunMode,S_TYPE,Shift,STD,curr_S_Type,Reporting_Date, Shift_TS)
VALUES(NULL, New.NAME, New.TS, NEW.Left_IO, New.Left_NIO, New.Recovery, New.Right_IO, New.Right_NIO, New.RunMode, New.S_TYPE,


(select @SHIFTIME),

(Select
`STD` From `oee_variant` Where `Machine_ID` = New.NAME And `S_TYPE` =

(Select
`S_TYPE`

From
`v_getmaxid`
Where
`NAME` = New.Name And
v_getmaxid.Max_id In (Select
Max(v_getmaxid.Max_id) As Max_Max_id
From
`v_getmaxid`
Where
`NAME` = New.Name))

And `oee_variant`.`Operators` =
(Select `Operators` from `oee_machinenames` where `ID` = New.NAME)),


(Select
`S_TYPE`

From
`v_getmaxid`
Where
`NAME` = New.Name And
v_getmaxid.Max_id In (Select
Max(v_getmaxid.Max_id) As Max_Max_id
From
`v_getmaxid`
Where
`NAME` = New.Name)),

(select((case when (((@SHIFTIME = 'Shift3') and
(cast(New.TS as time) >= '00:00:01'))
or (cast(New.TS as time) <= '05:59:00'))
then (New.TS - interval 1 day) else cast(New.TS as date) end)) AS Reporting_Date),

(SELECT @SHIFTSELECT)

);
END

最佳答案

不,您不能使用列名作为变量,除非它是动态查询(或)您在应用程序代码中进行查询。动态查询方式就像

SET @sql = CONCAT("SELECT ", @SHIFTIME," FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

然后将准备好的语句更改为

SET @result = '';
SET @sql = CONCAT("SELECT ? INTO @result FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME");
PREPARE stmt FROM @sql;
EXECUTE stmt USING @SHIFTIME;
SELECT @result; // Set the data to some other variable
DEALLOCATE PREPARE stmt;

编辑:啊!!没有注意到这是一个触发器。以为这是一个程序。是的,您不能只通过查询来获取 @SHIFTIME 和 SET @SHIFTSELECT := ... 这两个部分可以组合成如下所示的修改后的查询,因此不需要动态查询。

Select 

(Case
When ((CurTime() > oee_machinenames.Shift1) And
(CurTime() < oee_machinenames.Shift2)) Then oee_machinenames.Shift1
When ((CurTime() > oee_machinenames.Shift2) And
(CurTime() < oee_machinenames.Shift3)) Then oee_machinenames.Shift2
When ((CurTime() > oee_machinenames.Shift3) Or
(CurTime() < oee_machinenames.Shift1)) Then oee_machinenames.Shift3 End)
From
oee_machinenames
where oee_machinenames.ID = New.NAME

Group By
oee_machinenames.ID);

关于MySQL 从变量中选择列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628133/

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