例如,如果我有一个像这样的表结构,并且我想更新admin,那么我该怎么做?
+-----------+-----------+
| sender | recipient |
+-----------+-----------+
| admin | user1 |
| user1 | admin |
+-----------+-----------+
我尝试了什么?
UPDATE messages
SET (sender='newname' OR recipient='newname')
WHERE (sender='admin' OR recipient='admin')
错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您有一个 您的SQL语法错误;检查与您的手册相对应的手册 MySQL服务器版本可在附近使用正确的语法 '(sender ='dsfdsx'OR收件人='dsfdsx')在哪里(sender ='admin'或 第1行上的receiver ='admi'
应该更改什么?
答案 0 :(得分:3)
您可以相应地使用条件表达式CASE..WHEN
至UPDATE
:
UPDATE messages
SET
sender = CASE sender WHEN 'admin' THEN 'newname' ELSE sender END,
recipient = CASE recipient WHEN 'admin' THEN 'newname' ELSE recipient END
WHERE
sender = 'admin' OR recipient = 'admin'