简体   繁体   English

在不更新旧行的情况下将新行插入表中

[英]inserting new rows to table without updating old ones

Alright, i have revised the question to also include what i have so far, and what i want to do. 好吧,我已经修改了问题,使其也包括我到目前为止所拥有的以及我想做什么。 So here goes it: 因此,它去了:

CREATE ORDER (
product_nat_id int(3) NOT NULL,
name VARCHAR(20),
PRIMARY KEY (product_nate_id)
)
INSERT INTO ORDER(product_nat_id, name) VALUES(1, 'Product 1');
INSERT INTO ORDER(product_nat_id, name) VALUES(2, 'Product 2');
INSERT INTO ORDER(product_nat_id, name) VALUES(3, 'Product 3');

CREATE TABLE INT_PRODUCT (
product_id INTEGER NOT NULL AUTO_INCREMENT,
product_nat_id INTEGER NOT NULL,
title TINYTEXT,
dateCreate TIMESTAMP CURRENT_TIMESTAMP,
CONSTRAINT INT_PRODUCT_PK PRIMARY KEY (product_id),
UNIQUE INT_PRODUCT_NK (product_nat_id));

But what i want is, whenever a record arrives with an updated value but duplicate key, i need to insert it (and not updated), but avoid duplicate constraint based on the difference in time inserted. 但是我想要的是,每当一条记录有更新值但有重复的键到达时,我都需要将其插入(而不是更新),但要避免基于插入时间差的重复约束。 Hope this makes sense now. 希望这现在有意义。

I would suggest the following: 我建议以下内容:

  1. Look up the previous record. 查找上一条记录。 I assume you should know what that would be 我想你应该知道那会是什么

    SELECT Count(*) FROM dim WHERE recordId = '$recordid'

  2. If in step 1 the records returned are larger than 0 then invalidate the 'previous' record: 如果在步骤1中返回的记录大于0,则使“上一个”记录无效:

    UPDATE dim SET datevalid = '$datevalue' where recordId = '$recordid' and status = 2

  3. Continuing with step 1 where the ecords return in the check are larger than 0 now do the insert: 继续执行步骤1,其中支票中的ecord返回大于0,现在执行插入操作:

    INSERT INTO dim (recordId,field1,field2,date,status) VALUES (1,'sad','123123','2013-03-26',1)

  4. If step 1 was false then just do the insert: 如果步骤1为假,则执行插入操作:

    INSERT INTO dim (recordId,field1,field2,date,status) VALUES (1,'sad','123123','2013-03-26',1)

I would add a status field just as an extra measure when you need to find records and distinguish between valid or invalid then you do not need to filter between dates. 当您需要查找记录并区分有效还是无效时,我将添加一个status字段,作为一种额外的措施,那么您就不需要在日期之间进行过滤。 You can then use the status field. 然后,您可以使用status字段。 Also have a unique auto-increment key for every record even though the data might be the same for a set of valid and invalid records. 对于每条记录,还应具有唯一的自动递增键,即使一组有效和无效记录的数据可能相同。 recordId and unique key will not be the same in this case. 在这种情况下, recordId和唯一键将不同。 You assign the recordId and the system will assign the unique key on the table. 您分配了recordId ,系统将在表上分配唯一键。 status = 1 is valid and status = 2 is invalid. status = 1有效,而status = 2无效。

Hope this helps! 希望这可以帮助!

sample code of your post like as: 您的帖子的示例代码如下:

Insert query syntax looks like this: 插入查询语法如下所示:

INSERT INTO table (primarykeycol,col1,col2) 
VALUES (1,2,3) ON DUPLICATE KEY UPDATE col1=0, col2=col2+1

If there is already a row with primarykeycol set to 1 this query is equal to: 如果已经存在将primarykeycol设置为1的行,则此查询等于:

UPDATE table SET col1=0, col2=col2+1 WHERE primarykeycol = 1

explanation as: 解释为:

  1. Ordinarily to achieve the same result you would have to issue an UPDATE query, then check if there were affected rows and if not issue an INSERT query. 通常,要获得相同的结果,您将必须发出UPDATE查询,然后检查是否有受影响的行,如果没有,则发出INSERT查询。

  2. This way, you can do everything in one step – first try insert and then update if insert fails. 这样,您可以一步完成所有操作–首先尝试插入,然后在插入失败时进行更新。

  3. One situation for which this type of syntax is perfect is when you work with daily counters. 这种语法最适合的一种情况是使用每日计数器。 For example, you might have a table with PostID, Date and Count columns. 例如,您可能有一个包含PostID,Date和Count列的表。

  4. Each day you'd have to check if you already created an entry for that day and if so increase the count column – and this can be easily substituted with one INSERT … ON DUPLICATE KEY UPDATE query. 每天,您都必须检查是否已为该天创建了一个条目,如果需要,则增加count列-可以很容易地用一个INSERT…ON DUPLICATE KEY UPDATE查询代替。

  5. Unfortunately there are some caveats. 不幸的是,有一些警告。 One being that when you have multiple unique indexes it will act as if you had an OR condition in WHERE clause of UPDATE query. 一种是当您有多个唯一索引时,它将像在UPDATE查询的WHERE子句中具有OR条件一样。

  6. This means that multiple rows should be update, but INSERT … ON DUPLICATE KEY UPDATE will update only one row. 这意味着应该更新多行,但是INSERT…ON DUPLICATE KEY UPDATE将只更新一行。

MySQL manual: INSERT ON DUPLICATE KEY UPDATE Syntax MySQL手册: 插入重复的密钥更新语法

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

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