mysql字段为空时不更新_更新MySql字段(如果字段不为空,则转到下一个字段)

您必须使用case仅更新选择列。

例:

update table_name set

col1 = ( case when col1 is null then ? else col1 end )

, col2 = ( case when col2 is null then ? else col2 end )

-- , col3 = ...

;使用mysql prepare来替换查询参数。

另请参阅Quassnoi's answer to a similar posting。

更新:

IF ColumnB is not empty then update ColumnC, if ColumnC is not empty update ColumnD ...until it finds an empty column.

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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值