如何在MySQL更新查询中添加条件语句?

时间:2019-06-21 07:43:45

标签: mysql sql sql-update

我有以下mySQL更新查询。我该如何添加条件WHEN语句,其中说如果Status =“ PS”更改为“ PE”,并且Status =“ DS”更改为“ DE”

我不确定如何用正确的语法编写它。

 UPDATE Equipment SET delivery_time = "5 pm",
 stop_sequence = "Stop 2" 
WHERE (status = "Fixed" or status = "Broken")
 AND customer = "Mary" AND driver = "Pete"

2 个答案:

答案 0 :(得分:0)

用例何时

 UPDATE Equipment SET delivery_time = '5 pm',
 stop_sequence = 'Stop 2',
 status = case  status when 'PS' then 'PE' when 'DS' Then 'DE'
          else status end
 WHERE status in ('Fixed' 'Broken')
 AND customer = 'Mary' AND driver = 'Pete'

答案 1 :(得分:0)

使用if语句

更新设备SET delivery_time ='5 pm',stop_sequence ='Stop 2', 状态= IF(状态='PS','PE',IF(状态='DS','DE','NULL') ('PS''DS')中的WHERE状态  AND customer ='Mary'AND driver ='Pete'