SQL >> Database teknologi >  >> RDS >> Mysql

Sådan bruger du ON DUPLICATE KEY UPDATE

Brug syntaksen til at indsætte eller opdatere:

cur.execute("""
  INSERT INTO logs (a, b, c)
  VALUES (%s, %s, %s)
  ON DUPLICATE KEY UPDATE a=%s, c=%s
""", (line[0], line[1], line[2], line[0], line[2]))

De ekstra parametre leverer værdierne til UPDATE-delen, når nøglen allerede findes.

  1. On duplicate key ignore i MySQL

  2. ON DUPLICATE KEY UPDATE i SQLAlchemy

  3. Vilkårlig INSERT ... ON DUPLICATE KEY UPDATE

  4. Insert, on duplicate update i PostgreSQL?