Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Web Development
You last visited: Today at 08:47

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[MySQL] insert nur, wenn Eintrag nicht existiert

Discussion on [MySQL] insert nur, wenn Eintrag nicht existiert within the Web Development forum part of the Coders Den category.

Reply
 
Old   #1

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
[MySQL] insert nur, wenn Eintrag nicht existiert

Moin,

ich suche eine recht schnelle und simple Methode um Einträge in MySQL nur dann hinzuzufügen, wenn sie bereits nicht existieren (nach einer ID identifizierbar), sonst wird der Eintrag aktuallisiert.

Natürlich könnte ich das mit PHP ganz einfach über 3 Befehle lösen ... aber bei mehreren hunderten Einträgen wäre das nicht gerade aus schnell.

Kennt da jemand eine schnelle Möglichkeit zu?

MfG
Mr.Tr33
Mr.Tr33 is offline  
Old 11/13/2014, 23:50   #2
 
Mikesch01's Avatar
 
elite*gold: 203
Join Date: Sep 2007
Posts: 732
Received Thanks: 190
Was willst du denn genauer aktualisieren? Wenn das eine einmalige Sache ist, dann sollte das mit der Ladezeit nicht schlimm sein.

Gruß
Mikesch01 is offline  
Old 11/13/2014, 23:54   #3

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Das ist eig. ein Cronjob von mir der alle 5 Minuten aufgerufen wird und Einträge überprüft ob diese immer noch identisch sind oder ob halt neue dazu gekommen sind. Deswegen sollte es auch recht schnell funktioniert
Mr.Tr33 is offline  
Old 11/13/2014, 23:55   #4

 
Syc's Avatar
 
elite*gold: 666
Join Date: Apr 2011
Posts: 5,810
Received Thanks: 2,418
Syc is offline  
Old 11/14/2014, 00:08   #5

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Quote:
Originally Posted by Sycophantic View Post
Ich finde es jetzt sehr peinlich meinerseits
Ich bin schon so oft auf diesen Link gestoßen, doch irgend wie habe ich das "ON DUPLICATE KEY UPDATE" jedes mal überlesen oder nicht wahr genommen

Danke, funktioniert jetzt alles so wie es sollte. Ich hoffe die Geschwindingkeit ist auch ausreichend (habe es noch nicht ganz getestet)
Mr.Tr33 is offline  
Old 12/08/2014, 16:48   #6

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
So nochmal auf das Thema zurück zu greifen.

Ich habe eine Tabelle mit den Spalten id, name, type und id hat einen Primary Key und Autoincrese (oder so).
Jetzt möchte ich etwas in die Tabelle eintragen aber nur, wenn der "name" bereits nicht existiert und wenn er es doch tut dann soll "type" aktuallisiert werden.

Wie bekomme ich das hin in nur einem SQL Query?
Mr.Tr33 is offline  
Old 12/08/2014, 17:34   #7


 
Menan's Avatar
 
elite*gold: 0
The Black Market: 169/0/0
Join Date: Sep 2008
Posts: 9,484
Received Thanks: 3,109
Code:
INSERT INTO table (name, id, type) VALUES("Name", id, Type) ON DUPLICATE KEY UPDATE type=VALUES(type)
Menan is offline  
Old 12/08/2014, 17:41   #8

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Dann erstellt er bei mir einen neuen Eintrag ... was ich nicht möchte.
Mr.Tr33 is offline  
Old 12/08/2014, 17:43   #9


 
Menan's Avatar
 
elite*gold: 0
The Black Market: 169/0/0
Join Date: Sep 2008
Posts: 9,484
Received Thanks: 3,109
Quote:
Originally Posted by Mr.Tr33 View Post
Dann erstellt er bei mir einen neuen Eintrag ... was ich nicht möchte.
Brauchst du das in einem Query? Oder gehts auch in 2?

Du müsstest der Spalte Name eben einen UNIQUE Key geben.

Oder per:

Code:
IF EXISTS(SELECT type WHERE name=Value(name) FROM tabelle )
then
  UPDATE tabelle
  SET type = Value(type) 
  WHERE name=Value(Name)
ELSE 
  INSERT INTO tabelle (id, name, type)
  VALUES (id, name, type);
Menan is offline  
Old 12/08/2014, 18:18   #10

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Möchte es in einem Query

Und wie gebe ich einer text Spalte einen UNIQUE Key?
Mr.Tr33 is offline  
Old 12/08/2014, 22:45   #11
 
Mikesch01's Avatar
 
elite*gold: 203
Join Date: Sep 2007
Posts: 732
Received Thanks: 190
Hi,

das ist eigentlich eine Sache von PHP und Co. die serverseitig geprüft werden und dementsprechend reagieren soll.

Hierzu sollten 2 SQL Statements, je 1 nach Verzweigung, benutzt werden.

Gruß
Mikesch01 is offline  
Old 12/09/2014, 18:51   #12



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
Code:
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  ...
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ;
Lässt sich ansonsten auch via phpmyadmin o.Ä. im Nachhinein konfigurieren.
Shawak is offline  
Old 12/10/2014, 18:46   #13

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Hmm, ich finde jetzt nicht den Fehler. Woran könnte es liegen?

Code:
mysql> IF EXISTS(SELECT `type` WHERE `wort`= Value(`wort`) FROM `words`)
THEN
  UPDATE `words`
  SET `type` = Value(`type`), `update` = Value(`update`)
  WHERE `wort`= Value(`wort`)
ELSE 
  INSERT INTO `words` (`wort`, `type`, `update`)
  VALUES ('ich', 'Pronomen', '1418036206');
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS(SELECT `type` WHERE `wort`= Value(`wort`) FROM `words`)
THEN
  UPDAT' at line 1
Mr.Tr33 is offline  
Old 12/10/2014, 19:01   #14
 
Mikesch01's Avatar
 
elite*gold: 203
Join Date: Sep 2007
Posts: 732
Received Thanks: 190
Die Reihenfolge der SQL-Angabe im SELECT-Befehl ist falsch.

Erst kommt SELECT .. dann FROM ... und dann erst WHERE ...
Mikesch01 is offline  
Old 12/10/2014, 19:03   #15

 
Mr.Tr33's Avatar
 
elite*gold: 2778
Join Date: Feb 2012
Posts: 3,527
Received Thanks: 1,044
Joa das dachte ich am Anfang auch aber anscheinend ist es nicht der Fehler :/
Code:
mysql> IF EXISTS(SELECT `type` FROM `words`  WHERE `wort`= Value(`wort`))
THEN
  UPDATE `words`
  SET `type` = Value(`type`), `update` = Value(`update`)
  WHERE `wort`= Value(`wort`)
ELSE 
  INSERT INTO `words` (`wort`, `type`, `update`)
  VALUES ('ich', 'Pronomen', '1418036206');
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS(SELECT `type` FROM `words`  WHERE `wort`= Value(`wort`))
THEN
  UPDA' at line 1
Mr.Tr33 is offline  
Reply


Similar Threads Similar Threads
PHP / MYSQL Insert REAGIERT NICHT
05/05/2014 - Web Development - 9 Replies
Hallo leute, wollte mittels eines PHP-Scripts Daten in meine Tabelle einfügen. Es kommt auch keine Fehlermeldung wenn ich bsp. OR DIE ("Fail"); hinzufüge, allerdings werden die Einträge einfach nicht in die Tabelle übertragen. Erkennt ihr einen Fehler? <html> <head> <title> Test </title>
MySQL INSERT INTO trägt nicht alle Daten aus Formular ein
08/26/2012 - Web Development - 14 Replies
Hallo, ich wollte mir eine kleine Regi basteln. Dabei stoße ich auf das Problem, dass Username sowie Password eingetragen werden aber die E-Mail nicht. Hier einmal der komplette Code sowie DB: Das Formular: <link rel="stylesheet" type="text/css" href="../style.css" /> <h3>Registrierung</h3> <form action="index.php?page=reg" method="post" id="regist"> <table width="400" border="0" align="center"> <tr>
Mysql 5.5 fehler Account existiert bereits
06/07/2012 - Metin2 Private Server - 6 Replies
Hallo Ich habe seit dem ich Mysql 5.5 benutze den fehler beim hen! cms Account existiert bereits bei jede anmeldung aber account tabelle ist leer bei mysql 5.0 geht alles
[Release] MySQL Insert system
07/18/2010 - CO2 PServer Guides & Releases - 18 Replies
Ello! First of all, this is NOT sourcedependent, which means you can use this on any source without modifying the code. This is an replacement for all of you who still uses normal MySQL queries. Use this code to clean up, make it easier and more orginazed. Here's how it looks: Example: (Original (LOTF for example))
[HELP]C# MySQL Command insert if not exsist?
09/04/2009 - CO2 Private Server - 8 Replies
MySqlCommand Command = new MySqlCommand("INSERT INTO accounts(AccountID,LogonType) VALUES ('" + fillBox.Text + "','3')", Connection); Now, I want it to check if the value of fillBox.Text doesn't excist before making the account, so It wont create the same account. I tried making it in a try, that didn't help, it just make several accounts with the same name. ~Bas



All times are GMT +1. The time now is 08:48.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.