以下CASE
语句在BET1
的同时更新BET2
,BET3
,BET4
和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";
当我运行php页面时,我有BET1=32
,BET2=32
,BET3=32
,BET4=32
。
我想要的是让脚本在找到并填充第一个null
字段时退出。
即,从所有BET1
,BET2
,BET3
,BET4 = NULL when ID =10
开始,我希望在运行php页面时,只更新BET1=32
。当我再次运行它时,要更新BET2=32
等等
类似于以下内容:
$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";
结果
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.
如何编辑脚本以执行上述功能?
修改
@ InoS Heo
如果你有时间,最后一个问题。
在运行sql之前,我有$ S1 = 32。假设不是32,我有苏珊,这当然是一个字符串。
如何更新脚本以容纳上述字符串?
$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;
感谢。
答案 0 :(得分:1)
您正在寻找 "UPDATE first NULL column"
。我认为这可以使用MySQL用户变量来实现。
你可以尝试一下吗?
架构&数据强>
DROP TABLE IF EXISTS `TIMER-BOARD1`;
CREATE TABLE `TIMER-BOARD1` (
`ID` int(11) NOT NULL,
`BET1` int(11) DEFAULT NULL,
`BET2` int(11) DEFAULT NULL,
`BET3` int(11) DEFAULT NULL,
`BET4` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DELETE FROM `TIMER-BOARD1`;
INSERT INTO `TIMER-BOARD1` VALUES (10, NULL, NULL, NULL, NULL),
(10, 32, NULL, NULL, NULL), (10, 1, 2, NULL, NULL),
(10, 1, 2, 3, NULL), (10, 1, 2, 3, 4);
初始数据
SELECT * FROM `TIMER-BOARD1`;
+----+------+------+------+------+
| ID | BET1 | BET2 | BET3 | BET4 |
+----+------+------+------+------+
| 10 | NULL | NULL | NULL | NULL |
| 10 | 32 | NULL | NULL | NULL |
| 10 | 1 | 2 | NULL | NULL |
| 10 | 1 | 2 | 3 | NULL |
| 10 | 1 | 2 | 3 | 4 |
+----+------+------+------+------+
<强>更新强>
UPDATE `TIMER-BOARD1`
set
ID = @UPDATED := 10, -- user defined variable which keep track WHETHERE CHANGED OR NOT
BET1 = case when BET1 IS NULL THEN @UPDATED:= 32 else BET1 end,
BET2 = case when @UPDATED = 10 AND BET2 IS NULL THEN @UPDATED:=32 else BET2 end,
BET3 = case when @UPDATED = 10 AND BET3 IS NULL THEN @UPDATED:=32 else BET3 end,
BET4 = case when @UPDATED = 10 AND BET4 IS NULL THEN @UPDATED:=32 else BET4 end
WHERE ID = 10;
更新数据
SELECT * FROM `TIMER-BOARD1`;
mysql> SELECT * FROM `TIMER-BOARD1`;
+----+------+------+------+------+
| ID | BET1 | BET2 | BET3 | BET4 |
+----+------+------+------+------+
| 10 | 32 | NULL | NULL | NULL |
| 10 | 32 | 32 | NULL | NULL |
| 10 | 1 | 2 | 32 | NULL |
| 10 | 1 | 2 | 3 | 32 |
| 10 | 1 | 2 | 3 | 4 |
+----+------+------+------+------+
<强>问题强>
当你想要更新10时仍然有问题。
我认为你有两个选择
我想知道你是否有像string_col
这样的字符串列如下。
架构&amp;数据强>
DROP TABLE IF EXISTS tab;
CREATE TABLE tab (
`ID` int(11) NOT NULL,
`BET1` VARCHAR(100) DEFAULT NULL,
`BET2` VARCHAR(100) DEFAULT NULL,
`BET3` VARCHAR(100) DEFAULT NULL,
`BET4` VARCHAR(100) DEFAULT NULL,
`string_col` VARCHAR(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DELETE FROM tab;
INSERT INTO tab VALUES
(10, NULL, NULL, NULL, NULL, 'A'),
(10, 'Alice', NULL, NULL, NULL, 'B'),
(10, 'Alice', 'Bob', NULL, NULL, 'C'),
(10, 'Alice', 'Bob', 'Charlie', NULL, 'D'),
(10, 'Alice', 'Bob', 'Charlie', 'Dave', 'E');
SELECT * FROM tab;
+----+-------+------+---------+------+------------+
| ID | BET1 | BET2 | BET3 | BET4 | string_col |
+----+-------+------+---------+------+------------+
| 10 | NULL | NULL | NULL | NULL | A |
| 10 | Alice | NULL | NULL | NULL | B |
| 10 | Alice | Bob | NULL | NULL | C |
| 10 | Alice | Bob | Charlie | NULL | D |
| 10 | Alice | Bob | Charlie | Dave | E |
+----+-------+------+---------+------+------------+
<强>更新强>
UPDATE tab
set
string_col = @ORIG_STRING_COL := (@UPDATED := string_col),
BET1 = IF(BET1 IS NULL, @UPDATED := 'Susan', BET1),
BET2 = IF(@UPDATED != 'Susan' AND BET2 IS NULL, @UPDATED := 'Susan', BET2),
BET3 = IF(@UPDATED != 'Susan' AND BET3 IS NULL, @UPDATED := 'Susan', BET3),
BET4 = IF(@UPDATED != 'Susan' AND BET4 IS NULL, @UPDATED := 'Susan', BET4),
string_col = @ORIG_STRING_COL
WHERE ID = 10;
<强> RESULT 强>
SELECT * FROM tab;
+----+-------+-------+---------+-------+------------+
| ID | BET1 | BET2 | BET3 | BET4 | string_col |
+----+-------+-------+---------+-------+------------+
| 10 | Susan | NULL | NULL | NULL | A |
| 10 | Alice | Susan | NULL | NULL | B |
| 10 | Alice | Bob | Susan | NULL | C |
| 10 | Alice | Bob | Charlie | Susan | D |
| 10 | Alice | Bob | Charlie | Dave | E |
+----+-------+-------+---------+-------+------------+
使用以下伪代码,您可以实现它。
$res = mysqli_query(SELECT pk_column FROM tab WHERE ID = 10);
while ($row = mysqli_fetch_assoc($res))
{
$pk_col = $row['pk_column'];
mysqli_query("SET @UPDATED := ''");
mysqli_query("UPDATE tab
SET
BET1 = IF(BET1 IS NULL, @UPDATED := 'Susan', BET1),
BET2 = IF(@UPDATED = '' AND BET2 IS NULL, @UPDATED := 'Susan', BET2),
BET3 = IF(@UPDATED = '' AND BET3 IS NULL, @UPDATED := 'Susan', BET3),
BET4 = IF(@UPDATED = '' AND BET4 IS NULL, @UPDATED := 'Susan', BET4)
WHERE pk_col = $pk_column
");
}
前面的代码可以实现客户端或存储过程。
答案 1 :(得分:0)
您只需检查其他字段是否已经null
s:
UPDATE TIMER-BOARD1
SET BET1= case when BET1 IS NULL THEN $S1 else BET1 end ,
BET2= case when BET1 IS NOT NULL AND BET2 IS NULL THEN $S1 else BET2 end,
BET3= case when BET1 IS NOT NULL AND BET2 IS NOT NULL AND BET3 IS NULL THEN $S1 else BET3 end,
BET4= case when BET1 IS NOT NULL AND BET2 IS NOT NULL AND BET3 IS NOT NULL AND BET4 IS NULL THEN $S1 else BET4 end
WHERE ID = 10