MySQL on duplicate key update用法

基本使用方法

java 复制代码
public static final String SQL_TQI_SINK =
            "insert into " + ConfigureContext.get(ConfigKeyConstants.MYSQL_TABLE_TQI) + " \n" +
                    "(`mile_km`, `mile_start_km`, `mile_start_m`, `is_out`, `tqi_alig_l`, \n" +
                    "`tqi_alig_r`, `tqi_surf_l`, `tqi_surf_r`, `tqi_lev`, `tqi_gaug`, `tqi_twis`, `tqi_sum`, \n" +
                    "`out_value`, `average_speed`, `tran_cd`, `sped_cd`, `valid_flag`, `valid`, `dete_ymd`, \n" +
                    "`dete_hms`, `sub_code`, `line_cd`, `line_nm`, `dir_cd`, `dir_nm`, `regi_dt`)\n" +
                    "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n" +
                    "on duplicate key update \n" +
                    "mile_km = values(mile_km), \n" +
                    "mile_start_km = values(mile_start_km), \n" +
                    "mile_start_m = values(mile_start_m), \n" +
                    "is_out = values(is_out), \n" +
                    "tqi_alig_l = values(tqi_alig_l), \n" +
                    "tqi_alig_r = values(tqi_alig_r), \n" +
                    "tqi_surf_l = values(tqi_surf_l), \n" +
                    "tqi_surf_r = values(tqi_surf_r), \n" +
                    "tqi_lev = values(tqi_lev), \n" +
                    "tqi_gaug = values(tqi_gaug), \n" +
                    "tqi_twis = values(tqi_twis), \n" +
                    "tqi_sum = values(tqi_sum), \n" +
                    "out_value = values(out_value), \n" +
                    "average_speed = values(average_speed), \n" +
                    "tran_cd = values(tran_cd), \n" +
                    "sped_cd = values(sped_cd), \n" +
                    "valid_flag = values(valid_flag), \n" +
                    "valid = values(valid), \n" +
                    "dete_ymd = values(dete_ymd), \n" +
                    "dete_hms = values(dete_hms), \n" +
                    "sub_code = values(sub_code), \n" +
                    "line_cd = values(line_cd), \n" +
                    "line_nm = values(line_nm), \n" +
                    "dir_cd = values(dir_cd), \n" +
                    "dir_nm = values(dir_nm)";

如果数据库有值,不想覆盖数据库的值,可以采用以下写法

mysql(ON DUPLICATE KEY UPDATE)字段值为空更新新值,不为空不更新

tableName: 表名

id: 主键(唯一键)

field_1: 更新的字段

原值为空更新

sql 复制代码
INSERT IGNORE INTO `tableName` ( `id`, `field_1` )
VALUES
	( '1', '100' ),
	( '2', '200' ) 
	ON DUPLICATE KEY UPDATE field_1 =
IF
	( tableName.field_1, tableName.field_1, VALUES ( field_1 ) )

新值大于旧值更新

sql 复制代码
INSERT IGNORE INTO `tableName` ( `id`, `field_1` )
VALUES
	( '1', '100' ),
	( '2', '200' ) 
	ON DUPLICATE KEY UPDATE field_1 =
IF
	( VALUES ( field ) > tableName.field_1, VALUES ( field_1 ), tableName.field_1 )
相关推荐
寒冰碧海27 分钟前
使用ArcMap或ArcGIS Pro连接达梦数据库创建空间数据库
数据库
金融OG1 小时前
99.15 金融难点通俗解释:毛利率vs营业利润率vs净利率
大数据·数据库·python·机器学习·金融
青草地溪水旁2 小时前
Debian或Ubuntu系统中重置MySQL的root密码
mysql·ubuntu·debian
xianwu5432 小时前
反向代理模块1
开发语言·网络·数据库·c++·mysql
sunny052962 小时前
StarRocks 安装部署
数据库
青草地溪水旁2 小时前
mysql_use_result的概念和使用案例
数据库·mysql
cqths2 小时前
.NET 9.0 的 Blazor Web App 项目、Bootstrap Blazor 组件库、自定义日志 TLog 使用备忘
数据库·c#·.net·web app
翻晒时光3 小时前
MySQL 8.0 备份与恢复全解析
mysql
程序研3 小时前
mysql之group by语句
数据库·mysql
HaoHao_0105 小时前
AWS Outposts
大数据·服务器·数据库·aws·云服务器