简体   繁体   English

插入SQL表或更新记录(如果存在)

[英]Insert into SQL table or update record if exists

I want to add a row to a database table, but if a row exists with the same unique key, I want to update the row. 我想在数据库表中添加一行,但如果存在具有相同唯一键的行,我想更新该行。

Here is my query: 这是我的查询:

$query = "INSERT INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('$audit_no','$form_details_subquestion_id','$form_details_section_id','$mark') 
ON DUPLICATE KEY UPDATE mark = VALUES($mark)";

But, it will keep on inserting new record. 但是,它将继续插入新的记录。 Let's say audit_section_id, form_details_subquestion_id, form_details_section_id is unique key. 假设audit_section_id, form_details_subquestion_id, form_details_section_id是唯一键。 If audit_section_id, form_details_subquestion_id, form_details_section_id exists it will not insert new record into the database; 如果audit_section_id, form_details_subquestion_id, form_details_section_id存在audit_section_id, form_details_subquestion_id, form_details_section_id会将新记录插入数据库; while update the record. 同时更新记录。

I also tried this: 我也试过这个:

REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_‌​details_section_id`,`mark`)
VALUES ('$audit_no','$form_details_subquestion_id','$form_details_section_id','$mark')

I not sure is that the for loop problem of my code. 我不确定是我的代码的for循环问题。

    for ($i=0; $i < ($_POST['count']); $i++)
{
    $form_details_subquestion_id    = $_POST['form_details_subquestion_id'][$i];
    $form_details_section_id        = $_POST['form_details_section_id'][$i];
    $mark                           = $_POST['mark'][$i];
    $remark                         = $_POST['remark'][$i]; 


    //$query = "INSERT INTO `table` (`id`, `name`, `email`) VALUES (' ".$_POST['id']." ', ' ".$_POST['name']." ', ' ".$_POST['email']." ')";    

    $query = "REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`)
    VALUES ('$audit_no','$form_details_subquestion_id','$form_details_section_id','$mark')";
    $result = $db->query($query);
}

I'm using for loop to insert all the data. 我正在使用for循环来插入所有数据。

Here is the output for echo $query; 这是echo $ query的输出;

  REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','89','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','86','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','87','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','88','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','85','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','83','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','84','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','81','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','82','1','1.00')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','98','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','99','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','100','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','101','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','102','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','96','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','97','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','90','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','91','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','92','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','93','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','94','1','2')
REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','95','1','2')

SQL table SQL表

   CREATE TABLE IF NOT EXISTS `audit_section_markrecord` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `audit_section_id` int(10) unsigned NOT NULL,
  `form_details_subquestion_id` int(10) unsigned NOT NULL,
  `form_details_section_id` int(10) unsigned NOT NULL,
  `mark` decimal(5,2) unsigned NOT NULL,
  `dateofmodify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
)

Edit after your last question edit: 编辑完最后一个问题后编辑:

you have in your table this: 你在桌子上有这个:

`id` int(11) NOT NULL AUTO_INCREMENT,
....
 PRIMARY KEY (`id`)

Your id (PRIMARY KEY) is autoincremental and your question is: 你的id (PRIMARY KEY)是自动增量的,你的问题是:

I want to add a row to a database table, but if a row exists with the same unique key, I want to update the row. 我想在数据库表中添加一行,但如果存在具有相同唯一键的行,我想更新该行。

With your querys your never will have the same id because you never set the id value in your query, your query is: 使用您的查询,您永远不会拥有相同的id因为您从未在查询中设置id值,您的查询是:

REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`) VALUES ('602633','95','1','2')

and if you don't set the id value in your query the database system autoincrement automatically the id column value, and you never will have to update a row. 如果您没有在查询中设置id值,数据库系统会自动自动增加id列值,您永远不必更新行。


This should be your query: 这应该是您的查询:

$query = "REPLACE INTO audit_section_markrecord(`audit_section_id`,`form_details_subquestion_id`,`form_details_section_id`,`mark`)
    VALUES ('$audit_no','$form_details_subquestion_id','$form_details_section_id','$mark')";

But the audit_section_id , form_details_subquestion_id and form_details_section_id column values must match those of an existing row for the row to be replaced; 但是audit_section_idform_details_subquestion_idform_details_section_id列值必须与要替换的行的现有行的值匹配; otherwise, a row is inserted. 否则,插入一行。

You can read more at: 您可以在以下网址阅读更多

http://dev.mysql.com/doc/refman/5.7/en/replace.html http://dev.mysql.com/doc/refman/5.7/en/replace.html

EXAMPLE: 例:

Consider the table created by the following CREATE TABLE statement: 考虑以下CREATE TABLE语句创建的表:

CREATE TABLE test (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  data VARCHAR(64) DEFAULT NULL,
  ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id)
);

When you create this table and run the statements shown in the mysql client, the result is as follows: 当您创建此表并运行mysql客户端中显示的语句时,结果如下:

mysql> REPLACE INTO test VALUES (1, 'Old', '2014-08-20 18:47:00');
Query OK, 1 row affected (0.04 sec)

mysql> REPLACE INTO test VALUES (1, 'New', '2014-08-20 18:47:42');
Query OK, 2 rows affected (0.04 sec)

mysql> SELECT * FROM test; 
+----+------+---------------------+
| id | data | ts                  |
+----+------+---------------------+
|  1 | New  | 2014-08-20 18:47:42 |
+----+------+---------------------+
1 row in set (0.00 sec)

Now if you create a second table almost identical to the first, except that the primary key now covers 2 columns, as shown here ( PRIMARY KEY (id, ts) ): 现在,如果你创建一个几乎与第一个表相同的第二个表,除了主键现在覆盖2列,如此处所示( PRIMARY KEY (id, ts) ):

CREATE TABLE test2 (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  data VARCHAR(64) DEFAULT NULL,
  ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id, ts)
);

When you run on test2 the same two REPLACE statements as we did on the original(first) test table, we obtain a different result: 当您在test2上运行与原始(第一个)测试表上相同的两个REPLACE语句时,我们获得了不同的结果:

mysql> REPLACE INTO test2 VALUES (1, 'Old', '2014-08-20 18:47:00');
Query OK, 1 row affected (0.05 sec)

mysql> REPLACE INTO test2 VALUES (1, 'New', '2014-08-20 18:47:42');
Query OK, 1 row affected (0.06 sec)

mysql> SELECT * FROM test2;
+----+------+---------------------+
| id | data | ts                  |
+----+------+---------------------+
|  1 | Old  | 2014-08-20 18:47:00 |
|  1 | New  | 2014-08-20 18:47:42 |
+----+------+---------------------+
2 rows in set (0.00 sec)

This is due to the fact that, when run on test2, both the id and ts column values must match those of an existing row for the row to be replaced; 这是因为,当在test2上运行时,id和ts列值必须与要替换的行的现有行的值匹配; otherwise, a row is inserted. 否则,插入一行。

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

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