¿En ignorar la clave duplicada? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

Estoy tratando de terminar esta consulta; mi campo de etiqueta está establecido en ÚNICO y simplemente quiero que la base de datos ignore cualquier etiqueta duplicada.

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c')
ON DUPLICATE KEY IGNORE '*the offending tag and carry on*'

O incluso esto sería aceptable

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c')
ON DUPLICATE KEY UPDATE '*the offending tag and carry on*'
 128
Author: halfer, 2010-03-03

2 answers

Sugeriría NO usar INSERT IGNORE ya que ignora TODOS los errores (es decir, es un descuidado global ignore). En su lugar, ya que en su ejemplo tag es la clave única, use:

INSERT INTO table_tags (tag) VALUES ('tag_a'),('tab_b'),('tag_c') ON DUPLICATE KEY UPDATE tag=tag;

En la clave duplicada produce:

Consulta OK, 0 filas afectadas (0.07 seg)

 295
Author: thummper,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-07 11:14:52

Mysql tiene este práctico comando ACTUALIZAR A ;)

editar Parece que lo renombraron para REEMPLAZAR

REEMPLAZAR funciona exactamente como INSERTAR, excepto que si una vieja fila en la tabla tiene el mismo valor que una nueva fila para CLAVE PRIMARIA o un índice ÚNICO, el la fila antigua se elimina antes de la nueva fila se inserta

 11
Author: Byron Whitlock,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-02 21:16:15