我面临着重复密钥更新时插入的问题,它每次都会插入新记录而不是更新现有记录
- >所有日期列都是日期时间
- >所有记录都将以小写形式插入,例如(油,轮胎)
- >我正在比较更新中的任何唯一或主键
- > vehicle_id是外键
TABLE:
id int PK AI
vehicle_id int
maintenance_type varchar
price double
maintenance_date datetime
maintenance_start_date datetime
maintenance_end_date datetime
查询:
INSERT INTO `maintenance`
(`vehicle_id`,`maintenance_type`,`price`,`maintenance_date`,`maintenance_start_date`,`maintenance_end_date`)
VALUES
(1, "oil", "3520.00", "2015-11-25 00:00:00", "2015-11-04 00:00:00", "2015-11-13 00:00:00")
ON DUPLICATE KEY UPDATE
vehicle_id=VALUES(vehicle_id),
maintenance_type = VALUES(maintenance_type)
price = VALUES(price),
maintenance_date = VALUES(maintenance_date),
maintenance_start_date = VALUES(maintenance_start_date),
maintenance_end_date = VALUES(maintenance_end_date)
参考。链接: