以下代码仅在给定ID为null时更新字段。
$sql="UPDATE TABLE7 set SCORE1= $high where SCORE1 IS NULL AND ID = 9";
如何调整代码,使其仅在null时更新行,如果不为null,则检查下一行是否为某个ID = 9等。 类似的东西:
$sql="UPDATE TABLE7 set
IF SCORE1 IS NULL THEN SCORE1= $high,
ELSE //MEANING SCORE1 IS NOT NULL
IF SCORE2 IS NULL THEN SCORE2= $high,
ELSE // MEANING SCORE2 IS NOT NULL
IF SCORE3 IS NULL THEN SCORE3= $high
ELSE //MEANING ALL FIELDS ARE NOT NULL
THROW ERROR (ECHO ‘All FIELDS ARE FILLED’)
WHERE ID = 9"
谢谢。
修改
我想要的是,在更新第一个NULL字段后,其他字段将不会更新。
即如果SCORE1 = NULL,则SCORE1 = $ high但SCORE2和SCORE3不应更新,无论是否为NULL。
我只想更新第一个NULL字段。我怎样才能做到这一点?感谢。
答案 0 :(得分:0)
UPDATE TABLE7
set SCORE1 = case when SCORE1 IS NULL THEN $high else SCORE1 end,
SCORE2 = case when SCORE2 IS NULL and SCORE1 is not null THEN $high else SCORE2 end,
SCORE3 = case when SCORE3 IS NULL and SCORE1 is not null and score2 is not null THEN $high else SCORE3 end
WHERE ID = 9
and (score1 is null or score2 is null or score3 is null)