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 )
相关推荐
C吴新科2 小时前
MySQL入门操作详解
mysql
Ai 编码助手4 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员5 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle5 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻5 小时前
MySQL排序查询
数据库·mysql
萧鼎5 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^5 小时前
数据库连接池的创建
java·开发语言·数据库
苹果醋35 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
荒川之神5 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
IT培训中心-竺老师5 小时前
Oracle 23AI创建示例库
数据库·oracle