我有这样的默认程序:
PROCEDURE PMS_Device_Update_Last_log_date ( Device_ID VARCHAR(255),option_value DATETIME)
BEGIN
UPDATE PMS_Device
SET PMS_Device.last_log_date = option_value
WHERE PMS_Device.`Device_ID` = Device_ID;
我该怎么做:
SET PMS_Device.last_log_date = option_value if option_value <"2014-04-01" then
option_value = "2014-04-01"
if option_value > "2014-04-01" then SET PMS_Device.last_log_date = option_value
我有点失落,一直在线阅读但无法制作有效的语法
谢谢
答案 0 :(得分:0)
你应该把它放在一个变量中:
SET @optionValue =(CASE x WHEN 1那么'x'ELSE'y'END);
UPDATE x SET option_value = @optionValue WHERE a = b;
答案 1 :(得分:0)
如果要在单个查询中执行此操作,可以将查询框架化为:
IF option_value <= "2014-04-01"
BEGIN
UPDATE PMS_Device
SET last_log_date = "2014-04-01"
WHERE PMS_Device/Device_ID = Device_ID
END
ElSE //case: where option_value is greater than "2014-04-01"
BEGIN
UPDATE PMS_Device
SET last_log_date = option_value
WHERE PMS_Device/Device_ID = Device_ID
END
您也可以使用Case语句对其进行框架设置:
UPDATE PMS_DEVICE
SET last_log_date =
(case
when option_value < '2014-04-01'
then '2014-04-01'
end)
WHERE device_ID = 1
答案 2 :(得分:0)
您的查询格式应如下所示:
udapte tablename set columname = case when condition1 then answer1 when condition2 then answer2 else defaultvalue end