Sql-Exit CASE Statement when condition is met

Viewed 1058

The following CASE statement updates BET1, BET2, BET3 and BET4 at the same time when ID=10.

$S1=32
$sql="UPDATE TIMER-BOARD1 
set BET1= case when BET1 IS NULL THEN $S1 else BET1 end ,
    BET2= case when BET2 IS NULL THEN $S1 else BET2 end,
    BET3= case when BET3 IS NULL THEN $S1  else BET3 end,
    BET4= case when BET4 IS NULL THEN $S1 else BET4 end
WHERE ID = 10";

When I run the php page, I have BET1=32, BET2=32, BET3=32, BET4=32.

What I want is to have the script exit when it finds and fills the first null field.

I.e., starting with all BET1, BET2, BET3, BET4 = NULL when ID =10, I want when I run the php page, to update only BET1=32. And when I run it again, to update BET2=32, etc.

Something similar to the following:

    $S1=32
    $sql="UPDATE TIMER-BOARD1 
    set BET1= case when BET1 IS NULL THEN $S1 
( EXIT AND DON’T BOTHER ABOUT OTHER CASE      STATEMENTS) else BET1 end ,
       BET2= case when BET2 IS NULL THEN $S1 
(EXIT AND DON’T BOTHER ABOUT OTHER CASE STATEMENTS) else BET2 end,
    BET3= case when BET3 IS NULL THEN $S1 
(EXIT AND DON’T BOTHER ABOUT OTHER CASE STATEMENTS)   else BET3 end,
    BET4= case when BET4 IS NULL THEN $S1 
(EXIT AND DON’T BOTHER ABOUT OTHER CASE STATEMENTS) else BET4 end
    WHERE ID = 10";

RESULTS

Start values: ID=10 ,BET1=NULL, BET2=NULL, BET3=NULL, BET4=NULL.

Run the script 1ST time: ID=10, BET1=32, BET2 =NULL, BET3=NULL, BET4=NULL.

Run the script 2nd time: ID=10, BET1=32, BET2= 32, BET3=NULL, BET4 = NULL.

Run the script 3rd time: ID=10, BET1=32, BET2=32, BET3=32, BET4=NULL.

Run the script 4th time: ID=10, BET1=32, BET2=32, BET3=32, BET4=32.

Run the script 5th time or more: No null field, so no change.

How can I edit the script to perform the above function?

EDIT

@ InoS Heo

One final question if you have the time.

I have $S1=32 before running the sql. Suppose instead of 32 I have susan, which is of course a string.

How can your script be updated to accommodate strings like above?

$S1 =susan
UPDATE `TIMER-BOARD1`
set 
    ID = @UPDATED := 10,
    BET1 = case when BET1 IS NULL THEN @UPDATED:= susan else BET1 end,
    BET2 = case when @UPDATED = 10 AND BET2 IS NULL THEN @UPDATED:= susan else BET2     end,
…
    BET4 = case when @UPDATED = 10 AND BET4 IS NULL THEN @UPDATED:= susan else BET4 end
WHERE ID = 10;

Thanks.

2 Answers
Related