Bananas Development Blog: share your thoughts

MySQL INSERT und UPDATE mit einem Befehl

Sollte man mal den Fall haben Daten in eine Tabelle zu speichern und voher zu unterscheiden ob diese Daten schon vorhanden sind und aktualisiert werden müssen oder neu sind.

Dies lässt sich zeit MySQL 4.1 in einem Befehl machen
INSERT INTO access ('news','text','date') 
VALUES ('news tasda', 'tex ad asd t', '01.01.1900')
ON DUPLICATE KEY UPDATE news = 'neuerInhalt',
text = 'neuer Text',
Datum = 'neues Datum' ;


In diesem Beispiel sollte die Spalte news der Primär Schlüssel sein.
Mit einer vortlaufenden ID als Primärschlüssel geht das nicht.
Es muss ein eindeutger Wert sein, denn ansonsten funktioniert ON DUPLICATE KEY nicht.

MYSQL HandbuchReferenz

  • No comments

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Trackbacks / Pingbacks

  • No Trackbacks