开发者

Duplicate content

I want to update a table开发者_开发百科 SQL which content an ID + column already exist.

The problem is when I want to make the insert it gave me a message error:

**#1062 - Duplicate entry '35' for key 1**

So, I've try to make an UPDATE but it doesn't work too, because the syntax isn't correct the form of my request SQL is :

INSERT INTO `liens_series` 
(`id`, `id_series`, `password`) 
VALUES (35, '12', 
'<br>Saison 3 en cours de production'), .... 

The list is too long.


You might want to investigate the ON DUPLICATE KEY UPDATE clause of the INSERT statement in MySQL. Briefly, your statement might be changed to be

INSERT INTO `liens_series`
  (`id`, `id_series`, `password`)
VALUES
  (35, '12',  '<br>Saison 3 en cours de production')
ON DUPLICATE KEY UPDATE `id_series` = '12',
                        `password` = '<br>Saison 3 en cours de production')

Share and enjoy.


Use this: http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html It will do what you want, insert or update if neccessary.


If you insert a new row of which the id isn't determined yet:

INSERT INTO `liens_series` (`id`, `id_series`, `password`) VALUES (NULL, '12', '<br>Saison 3 en cours de production');

or

INSERT INTO `liens_series` (`id_series`, `password`) VALUES ('12', '<br>Saison 3 en cours de production');

The new id will be returned in LAST_INSERT_ID() (MySQL) or mysql_insert_id() (C, PHP etc.).


If you want to insert a row, and update its content if it already exists, use REPLACE INTO:

REPLACE INTO `liens_series` (`id`, `id_series`, `password`) VALUES (35, '12', '<br>Saison 3 en cours de production');

Note that behind the scenes, the row with id 35 will be deleted (if it exists) and then the INSERT issued. So you can't update the row only partially and the number of affected rows reflects the implicit number of DELETES + the number of INSERTS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜