0

这是我当前的查询

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

虽然还没有。

4

3 回答 3

2

你有点走错方向了,虽然这个逻辑应该在下一个布局中(在我看来)

尝试:

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";
于 2013-05-01T20:06:31.230 回答
2

我没有对此进行测试,所以不要强迫我这样做,但这应该为您指明正确的方向:

UPDATE records 
SET 
    tester1 = CASE WHEN tester1 IS NULL THEN '$user' ELSE tester1 END,
    test1 = CASE WHEN tester1 IS NULL THEN '$pass' ELSE test1 END,
    tester2 = CASE WHEN tester1 IS NOT NULL THEN '$user' ELSE tester2 END,
    tester2yn = CASE WHEN tester1 IS NOT NULL THEN '$pass' ELSE tester2yn END
WHERE
    id = $id
于 2013-05-01T21:40:00.297 回答
0

虽然它在 SQL 中是可行的,但这种逻辑不应该发生在数据库级别。用户的登录、身份验证和管理(这似乎是您所追求的)应该在您的代码中完成。

于 2013-05-01T20:06:04.120 回答