基本使用方法
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: 更新的字段
原值为空更新
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 ) )
新值大于旧值更新
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 )