插入mysql数据库,如果记录已存在,则更新

发布于 2024-11-05 11:51:50 字数 1312 浏览 1 评论 0原文

可能的重复:
怎么做我在 MySQL 中如果存在则更新,如果不存在则插入(又名 upsert 或合并)?

如何插入到 mysql 数据库,如果记录已存在,则更新...我知道此页面上有一个解决方案: http://dev.mysql.com/doc/ refman/5.1/en/insert-on-duplicate.html

sib_table

+=======================================================+
| sib_id | std_id  |  sib_name  |  sib_sex  |  sib_age  |
+=======================================================+
| 1      | 77      |  Sajjad    |   m       | 5/17/1990 |
| 1      | 77      |  Farah     |   f       | 9/14/1980 |
| 1      | 77      |  Bilal     |   m       | 1/10/1995 |
+=======================================================+

如果我想向该表添加另一个同级表,sql 会是什么。

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES 
  ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;

Possible Duplicate:
How do I update if exists, insert if not (aka upsert or merge) in MySQL?

how to insert into mysql database, if records already exists, then update...I know there is a solution on this page:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

sib_table

+=======================================================+
| sib_id | std_id  |  sib_name  |  sib_sex  |  sib_age  |
+=======================================================+
| 1      | 77      |  Sajjad    |   m       | 5/17/1990 |
| 1      | 77      |  Farah     |   f       | 9/14/1980 |
| 1      | 77      |  Bilal     |   m       | 1/10/1995 |
+=======================================================+

What sql would be if I want to add anther sibling to this table.

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;

INSERT INTO sib_table
  (std_id,sib_name,sib_sex,sib_age) 
VALUES 
  ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE id = LAST_INSERT_ID(id), c = 3;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情丝乱 2024-11-12 11:51:50

您已经很接近了,但您需要像对待 UPDATE 语句一样对待 ON DUPLICATE KEY 子句。这意味着您需要设置一个唯一的密钥,以便尝试执行以下操作:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  

...只能一次。然后添加 ON DUPLICATE KEY UPDATE 子句来更改其余字段(即不属于键的字段)以进行匹配。

因此,举例来说,假设我正确读取了表结构,如果您在 std_idsib_name 列上放置唯一的复合键,这将确保您无法添加两个同名的兄弟姐妹。 意味着当您像这样添加另一个时:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE sib_sex = 'f', sib_age = '1/27/2005'

...它将执行以下两件事之一:

  1. 如果 Aamna 在系列 #77 中不存在,则添加新行。
  2. 或者更新 Aamna 的性别和生日(如果之前已添加过)。

这种结构比 MySQL 的 REPLACE 更强大,因为它允许您对冲突行执行不同的操作,而不仅仅是用您尝试插入的内容覆盖它。当然,大多数时候 REPLACE 的功能才是真正想要的。但最好了解更通用的说法。

You're close, but you need to treat the ON DUPLICATE KEY clause just like an UPDATE statement. That means you need to setup a unique key so that trying to do:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Sajjad','m','1/5/2010')  

... will only work once. Then you add the ON DUPLICATE KEY UPDATE clause to change the rest of the fields (i.e. the ones that aren't part of the key) to match.

So, for example, assuming I read the table structure correctly, if you put a unique composite key on the columns std_id and sib_name, this will ensure that you can't add two siblings of the same name. And that means that when you go to add another like this:

INSERT INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')  
ON DUPLICATE KEY 
  UPDATE sib_sex = 'f', sib_age = '1/27/2005'

... it will do one of two things:

  1. add the new row if Aamna doesn't exist in family #77.
  2. or update Aamna's sex and birthday if she's been added before.

This structure is more powerful than MySQL's REPLACE because it lets you do something different to the conflicting row than merely overwrite it with what you tried to insert. Of course, most of the time the functionality of REPLACE is what is actually wanted. But it is better to know the more generic statement.

情感失落者 2024-11-12 11:51:50

请尝试以下操作:

REPLACE INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')

Try the following:

REPLACE INTO sib_table (std_id,sib_name,sib_sex,sib_age) 
VALUES ('77','Aamna','f','1/27/2005')
倾城月光淡如水﹏ 2024-11-12 11:51:50

检查案例中的行数:

SELECT * where ...

if (number of rows == 0) {
   INSERT ...
} else {
   Update ...
}

check number of rows in a case:

SELECT * where ...

if (number of rows == 0) {
   INSERT ...
} else {
   Update ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文