cod*_*ama 2 mysql sql sql-update
假设我有这张表:
ID | col1 | col2 | col3 | col4
1 | val | | val |
Run Code Online (Sandbox Code Playgroud)
有没有办法修改此查询:
UPDATE table set col1 = "bla", col2 = "bla", col3 = "bla", col4 = "bla where id = 1
Run Code Online (Sandbox Code Playgroud)
所以我最终得到:
ID | col1 | col2 | col3 | col4
1 | val | bla | val | bla
Run Code Online (Sandbox Code Playgroud)
换句话说,查询必须只更新非空字段.你是怎样做的?
最简单的答案是使用 COALESCE
UPDATE table
set col1 = COALESCE(col1,"bla"),
col2 = COALESCE(col2,"bla"),
col3 = COALESCE(col3,"bla"),
col4 = COALESCE(col4,"bla")
where id = 1
Run Code Online (Sandbox Code Playgroud)
其他链接.
| 归档时间: |
|
| 查看次数: |
1841 次 |
| 最近记录: |