我有一张包含人物和地址的表格。这些地址经常被送到数据库并且它们不会改变,但是如果它们这样做,我想保留最后一个地址并添加新地址,并带有时间戳。
此表中没有主键。 person_id是人员表中的关键
我在其中尝试了很多东西:
INSERT INTO addresses (person_id,street,zip,timestamp) SELECT ("1","122
Farm", "4422",CURRENT_TIMESTAMP) FROM addresses WHERE NOT EXISTS
(SELECT person_id,street,zip FROM addresses WHERE person_id = "1" AND
street = "122 Farm" AND zip = "4422")
谢谢!
答案 0 :(得分:0)
我刚发现的解决方案是: 1-创建相关字段的唯一索引:
ALTER TABLE addresses ADD UNIQUE INDEX a_uindex (person_id,street,zip)
然后使用:
INSERT IGNORE INTO addresses VALUES ("1","122 Farm","4422", CURRENT_TIMESTAMP)