dongyo7931 2013-12-15 09:57
浏览 27
已采纳

Sql-Update字段仅在空ELSE检查下一行时

The following code only updates a field when null for a given ID.

$sql="UPDATE TABLE7 set SCORE1= $high where SCORE1 IS NULL AND ID = 9";

How can I adjust the code so it updates a row only when null, and if not null, checks next row for a certain ID=9 etc. Something like:

$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"

Thank you.

EDIT

What I want is that after updating the first NULL field, other fields will not be updated.

ie if SCORE1 =NULL, then SCORE1 = $high but SCORE2 and SCORE3 shouldn't update, whether NULL or not.

I only want to update first NULL field ONLY. How can I do that? Thanks.

  • 写回答

1条回答 默认 最新

  • dongzi0850 2013-12-15 10:01
    关注
    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度