简体   繁体   English

插入到使用相同的行(相同的主键)mysql php pdo

[英]insert into using same row(same primary key) mysql php pdo

INSERT INTO courses1 (sys_id,name, location) 
SELECT *
FROM courses 
WHERE sys_id= $sysid

I have the two tables above and I want to insert in course1 what I have in course.我有上面的两个表,我想在 course1 中插入我在课程中拥有的内容。 But I get problem with sys_id because I get Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'' in so what I did is a added a column in courses1 named system_id but then I get another error saying column not match.但是我遇到了sys_id问题,因为我Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'' in所以我所做的是在课程 1 中添加了一个名为system_id的列,但随后我收到另一个错误,说列不匹配。

Tried

INSERT IGNORE INTO but ther result is not what I am expecting. INSERT IGNORE INTO但结果不是我所期望的。 The result I want is to have the data from course to course1 everytime I make a transaction.我想要的结果是每次进行交易时都拥有从 course 到 course1 的数据。 I want the data not to be overriden.我希望数据不被覆盖。

  1. What is the best solution for insert into when inserting same row(same primary key)?什么是最好的解决方案insert into插入同一行(同一主键)时?

UPDATE

My aim is to back-up the data (historical data).我的目标是备份数据(历史数据)。 I want to record the data before editing it.我想在编辑之前记录数据。 basically what i am doing is inserting the data in another table before updating it基本上我正在做的是在更新它之前将数据插入另一个表中

This question is a perfect example that demonstrates the fact that to give an answer, one have to understand the context of the question, not just write an automated post triggered by some keyword in the question. 这个问题是一个完美的例子,它说明了一个事实, 即给出答案,不仅要理解问题的上下文,还必须写出由某个关键字触发的自动帖子。

If you want a history table, it have to have a different structure from the main table. 如果要使用历史记录表,则它必须具有与主表不同的结构。 At least it should have a non-unique key for the id from the main table. 至少对于主表中的ID,它应该具有非唯一键。

So, make a history table of the same structure, but add a main_id field. 因此,制作具有相同结构的历史记录表,但添加一个main_id字段。 That gives you a structure like 这给你一个像

id
manin_id
name
location

Then, to add a record into a history table, 然后,要将记录添加到历史记录表中,

INSERT INTO courses_history SELECT NULL, * FROM courses  WHERE sys_id=?

you may want also to add an index (non-unique) for the main_id field. 您可能还想为main_id字段添加索引(非唯一)。

This way it will allow to store multiple "replicas" of the same row from the main table. 这样,它将允许存储主表中同一行的多个“副本”。

To get the last replica, you need a query like this: 要获取最后一个副本,您需要这样的查询:

SELECT * FROM courses_history WHERE sys_id=? ORDER BY id desc

Make a PRIMARY KEY AUTO INCREMENT named courses1_id , add one more field in courses1 table named courses_id and change your query 做一个PRIMARY KEY AUTO INCREMENT命名courses1_id中,增加一个字段courses1表命名courses_id ,改变你的查询

CREATE TABLE `courses1` ( 
`courses1_id` INT(11) NOT NULL AUTO_INCREMENT , 
`courses_id` INT(11) NOT NULL , 
`name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
`location` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
PRIMARY KEY (`courses1_id`), 
INDEX (`courses_id`));

INSERT INTO `courses1` (`courses_id`,`name`, `location`) 
SELECT *
FROM `courses` 
WHERE `courses_id`= $id

OR just use a DATETIME field in your courses1 table to keep a reference of the date before change 或只使用courses1表中的DATETIME字段来保留更改前的日期参考

    CREATE TABLE `courses1` ( 
    `date_added` DATETIME NOT NULL , 
    `courses_id` INT(11) NOT NULL , 
    `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
    `location` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL , 
    INDEX (`courses_id`));

    INSERT INTO `courses1` (NOW(),`courses_id`,`name`, `location`) 
        SELECT *
        FROM `courses` 
        WHERE `courses_id`= $id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM