Ankündigung

Einklappen
Keine Ankündigung bisher.

mysql: INSERT und wenn vorhanden UPDATE

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • mysql: INSERT und wenn vorhanden UPDATE

    Hallo zusammen,

    der Titel sagt eigentlich schon fast alles.

    Wie löst ihr die Problematik eines INSERTs der zum UPDATE werden soll, wenn ein entsprechender UNIQUE KEY schon vorhanden ist am besten?

    REPLACE wäre eine tolle Möglichkeit, allerdings soll hier beim update noch ein Wert erhöht werden (count = count + 1), was REPLACE IMHO nicht möglich macht.

  • #2
    Mach ein INSERT, frag im Fehlerfall den mysql_errno() ab, ist dieser der des DUPLICATE ENTRY (den ich gerade nicht auswendig kenne) mach ein UPDATE.
    Anders gehts wohl nicht, wenn es mit REPLACE nicht klappt.

    Kommentar


    • #3
      ... INSERT INTO ... ON DUPLICATE KEY update col = col+1 ...

      Kommentar


      • #4
        ... INSERT INTO ... ON DUPLICATE KEY update col = col+1 ...
        Das isses Great... Danke!

        Kommentar


        • #5
          und warum steht das nicht in MYSQL? *argh*

          Kommentar


          • #6
            Zitat von Sclot
            und warum steht das nicht in MYSQL? *argh*
            doch, steht drin

            If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have identical effect:

            Code:
            mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
                -> ON DUPLICATE KEY UPDATE c=c+1;
            http://dev.mysql.com/doc/refman/5.0/en/insert.html
            PHP & Linux-Support uvm...

            Kommentar


            • #7
              Das bezog sich wohl eher auf das Forum hier, bevor Sclot moderierend eingriff.

              Kommentar


              • #8
                oh *G*
                PHP & Linux-Support uvm...

                Kommentar


                • #9
                  so geht´s auch

                  Hallo,

                  ich stand auch gerade vor dem Problem und hatte Schwierigkeiten ein komplettes Beispiel zu finden. Da ich diese Seite recht schnell gefunden habe und denke, daß es eventuell auch noch andere interessiert, hier eine mögliche Lösung.
                  Grundlage: Tabelle 'test' mit ID(int,autoinc), text1(varchar,50), text2(varchar,50)
                  Die SQL-Funktion:
                  Code:
                  DELIMITER $
                  CREATE DEFINER=`d01691b6`@`%` PROCEDURE `updins`(IN `t1` vARCHAR(50), IN `t2` vARCHAR(50), IN `mID` INT)
                  	LANGUAGE SQL
                  	NOT DETERMINISTIC
                  	CONTAINS SQL
                  	SQL SECURITY DEFINER
                  	COMMENT 'updates or inserts some value'
                  BEGIN
                  	if(SELECT count(*) FROM test WHERE ID=mID)>0 then
                  		UPDATE test SET text1 = t1,text2 = t2 WHERE ID=mID;
                  	else
                  		INSERT INTO test (text1,text2) VALUES (t1,t2);
                  	end if;
                  END$
                  DELIMITER ;
                  Aufruf per:
                  Code:
                  call updins('erster Text','zweiter Text',1...n);
                  Funktioniert bei mir prima. Hoffe es hilft auch anderen. Viel Spass damit.
                  Die ON DUPLICATE-Version ist zwar auch nicht schlecht und vor allem kürzer, aber in meinem Fall greife ich von einer speziellen Datenbank-Software auf MySQL zu und dafür ist das hier eher günstiger - meine ich.

                  Grüße,
                  Jochen
                  while(!asleep()){sheep++;}

                  Kommentar


                  • #10
                    Was soll daran ungünstig sein?
                    [URL="https://github.com/chrisandchris"]GitHub.com - ChrisAndChris[/URL] - [URL="https://github.com/chrisandchris/symfony-rowmapper"]RowMapper und QueryBuilder für MySQL-Datenbanken[/URL]

                    Kommentar


                    • #11
                      LEICHENFLEDDERER - der Thread ist über 7 1/2 Jahre alt
                      [Quote=nikosch]
                      So glatt kann doch wirklich keiner sein.[/quote] :roll:

                      Kommentar

                      Lädt...
                      X