当插入数据时,出现错误,或重复数据,将不返回错误,只以警告形式返回。如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据。
INSERT IGNORE INTO im_msg_send <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="platformType != null and platformType != ''"> platform_type, </if> ... </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=BIGINT}, </if> <if test="platformType != null"> #{platformType}, </if> ... </trim>当primary或者unique重复时,则执行update语句,否则新增。 tips:ON DUPLICATE KEY UPDATE后放置需要更新的数据,未放到此处的列不会被更新
INRSERT INTO `nursing_worker_score` ( `nursing_worker_id`, `assess_target_id`, `score`, `type`, `mtime` ) VALUES ( #{nursingWorkerId}, #{targetUserId}, #{score}, #{type}, now() ) ON DUPLICATE KEY UPDATE mtime = values(mtime), score = values(score)根据select的条件判断是否插入,可以不光通过primary 和unique来判断,也可通过其它条件。
INSERT INTO books (NAME) SELECT 'MySQL Manual' FROM DUAL WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)如果存在primary or unique相同的记录,则先删除掉。再插入新记录。
REPLACE INTO books SELECT 1, 'MySQL Manual' FROM booksTODO