这是我当前的查询

UPDATE `records` SET tester1 = '$user', tester1yn = '$pass'


我想将其更改为

UPDATE `records` SET
IF `tester1` IS NULL `tester1` = $user, AND `test1` = $pass
ELSE `tester2` = $user, `tester1yn` = '$pass'
WHERE `id` = $id


但是据我所知,这是行不通的。我有一个嵌套的问题,还是我将以错误的方式进行操作?

基于反馈的更新

UDPATE records SET
tester1 = if(tester1 IS NULL,'$user',tester1),
tester1yn = if(tester1 is null, '$pass', tester1yn),
tester2 = IF(tester1 is not null, '$user', tester2),
tester2yn = IF(tester1 is not null,'$pass', tester2yn)
where id = $id


仍然还没有。

最佳答案

尽管这种逻辑应该放在下一个布局中(我认为),但是您的前进方向很错误

尝试:

UPDATE records
SET
tester1 = if(tester1 IS NULL,$user,tester1),
tester1yn = IF(tester1 IS NULL,$pass,tester1yn),
tester2 = IF(tester1 IS NOT NULL, $user,tester2),
tester2yn = IF(tester1 IS NOT NULL,'$pass',tester2yn)
WHERE id = $id";

关于mysql - 如何在UPDATE语句中使用if/else,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16325499/

10-12 01:13