选择子查询中的MYSQL更新

时间:2015-08-15 06:04:05

标签: mysql sql-update subquery substr sql-subselect

“响应”列中的数据为= blahblahblahTYPE=ERRORblahblah。然后我运行下面的查询来定位'ERROR'字符串。

SELECT substr(Response, instr(Response, 'ERROR'), 5) 
  FROM table 
  WHERE id = 721451 
    AND Status = 'false' 
    AND Response LIKE '%<status>Unknown</status>%' 
    AND date >= curdate();

上面的查询返回:ERROR

我基本上想要将ERROR更新为SUCCESS而不修改其余数据。

下面的查询是否有效?有更好的方法吗?

UPDATE table 
  SET Response = 'SUCCESS' 
  WHERE (
    SELECT substr(Response, instr(Response, 'ERROR'), 5) 
      FROM table 
      WHERE id = 721451 
        AND Status = 'false' 
        AND Response LIKE '%<status>Unknown</status>%' 
        AND date >= curdate()
  );

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

UPDATE table 
  SET Response = REPLACE(Response, 'ERROR', 'SUCCESS')
   WHERE id = 721451 
     AND Status = 'false' 
     AND Response LIKE '%<status>Unknown</status>%' 
     AND date >= curdate();

参考https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace