gpt4 book ai didi

mysql - 如果前一列为空,则只更新一列(下一列)

转载 作者:行者123 更新时间:2023-11-29 01:04:24 26 4
gpt4 key购买 nike

我已经创建了表格并插入了如下值。

create table mytable (id INT, col1 INT, col2 INT, col3 INT);

insert into mytable values
(1,1,1,NULL),
(2,1,NULL,NULL);

我想做的是如果 col1 不为空则更新 col2,如果 col2 不为空则更新 col3 等等...但只有一列要更新。

假设我想更新 id=2 的数据,那么应该只更新 col2 而不是 col2、col3,因为它们都是空的。

当我尝试使用以下查询时,所有 列都会更新。

update myTable set  
col1 = ( IF (col1 is null, 9, col1) ),
col2 = ( IF (col2 is null, 9, col2) ),
col3 = ( IF (col3 is null, 9, col3) );

应该怎么做才能只更新一列。

最佳答案

您的查询似乎是对 Update MySql Field (if field is not empty, go to next one) 的扩展

您需要以相反的顺序设置列值检查。

update table_name set  
-- more next ( 4 to n ) columns here if required
col3 = ( case when ( col2 is not null and col3 is null ) then 7 else col3 end )
, col2 = ( case when ( col1 is not null and col2 is null ) then 8 else col2 end )
, col1 = ( case when ( col1 is null ) then 9 else col1 end )
;

示例表:

mysql> create table col_values ( id INT, col1 INT, col2 INT, col3 INT );
Query OK, 0 rows affected (0.06 sec)

mysql> insert into col_values values ( 1, 1, 1, NULL ), ( 2, 1, NULL, NULL ), ( 3, NULL, NULL, NULL );
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from col_values;
+------+------+------+------+
| id | col1 | col2 | col3 |
+------+------+------+------+
| 1 | 1 | 1 | NULL |
| 2 | 1 | NULL | NULL |
| 3 | NULL | NULL | NULL |
+------+------+------+------+
3 rows in set (0.00 sec)

mysql> update col_values set
-> col3 = ( case when ( col2 is not NULL and col3 is NULL ) then 7 else col3 end )
-> , col2 = ( case when ( col1 is not NULL and col2 is NULL ) then 8 else col2 end )
-> , col1 = ( case when ( col1 is NULL ) then 9 else col1 end )
-> ;
Query OK, 3 rows affected (0.02 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> select * from col_values;
+------+------+------+------+
| id | col1 | col2 | col3 |
+------+------+------+------+
| 1 | 1 | 1 | 7 |
| 2 | 1 | 8 | NULL |
| 3 | 9 | NULL | NULL |
+------+------+------+------+
3 rows in set (0.00 sec)

您还可以使用 if 函数来替代 case 语句。

mysql> truncate table col_values;
Query OK, 3 rows affected (0.01 sec)

mysql> insert into col_values values ( 1, 1, 1, NULL ), ( 2, 1, NULL, NULL ), ( 3, NULL, NULL, NULL );
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from col_values;
+------+------+------+------+
| id | col1 | col2 | col3 |
+------+------+------+------+
| 1 | 1 | 1 | NULL |
| 2 | 1 | NULL | NULL |
| 3 | NULL | NULL | NULL |
+------+------+------+------+
3 rows in set (0.00 sec)

mysql> update col_values set
-> col3 = if( ( col2 is not NULL and col3 is NULL ), 7, col3 )
-> , col2 = if( ( col1 is not NULL and col2 is NULL ), 8, col2 )
-> , col1 = if( col1 is NULL, 9, col1 )
-> ;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0

mysql> select * from col_values;
+------+------+------+------+
| id | col1 | col2 | col3 |
+------+------+------+------+
| 1 | 1 | 1 | 7 |
| 2 | 1 | 8 | NULL |
| 3 | 9 | NULL | NULL |
+------+------+------+------+
3 rows in set (0.00 sec)

关于mysql - 如果前一列为空,则只更新一列(下一列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11177532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com