我要求此查询根据CASE
返回的值执行COUNT
语句:
select @getshift := (select(COUNT(*) from `oee_machinenames` where `Shift1` = '06:00:00'))
(Case
When @getshift >=1
Then
update `oee_system`.`oee_machinenames`
set `oee_machinenames`.`default_GM_state` = 0
where `oee_machinenames`.`Shift1` = '06:00:00'
when @getshift ==0
then
update `oee_system`.`oee_machinenames`
set `oee_machinenames`.`default_GM_state` = 9
where `oee_machinenames`.`Shift1` = '06:00:00' end)
变量@getshift应该返回count的数量,如果这个>= 1
则发生更新语句,如果没有,那么应该发生另一个更新语句。我最终将在MYSQL时间触发事件中使用它
编辑:这两项都不能用作存储过程:
CREATE DEFINER=`tpm_dev`@`%` PROCEDURE `getshifts`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
select @getshift := select COUNT(*) from oee_machinenames where oee_machinenames.Shift1 = '06:00:00';
update oee_machinenames
if @getshift >=1
set default_GM_state = 99;
else
set default_GM_state = 90;
end if
END
答案 0 :(得分:1)
您可以加入子查询。但是,您正在更新与子查询使用相同的表,MySQL不支持该表。你可以放弃一个额外的子查询来从子查询中选择结果来隐藏MySQL中的公共表。
这样的事情: -
UPDATE oee_system.oee_machinenames a
INNER JOIN
(
SELECT aCount
FROM
(
SELECT COUNT(*) AS aCount
FROM oee_machinenames
WHERE Shift1 = '06:00:00'
) c
) b
set a.default_GM_state = IF(b.aCount = 0, 0, 99)
WHERE b.aCount >= 0
AND a.Shift1 = '06:00:00'