[MySQL] update en/of insert?

Pagina: 1
Acties:

  • Explore
  • Registratie: Maart 2001
  • Laatst online: 08-04-2011

Explore

Op zoek naar werk

Topicstarter
Is het met MySQL (en PHP) mogelijk om in 1 query een record toe te voegen, als een bepaald veld nog niet bestaat en te updaten als deze wel bestaat?

Of is het nodig dat er eerst een query op de db wordt gedaan, met een 'where' om na te gaan of het veld al in de table staat en afhankelijk van het resultaat te kiezen tussen een 'insert into' of een 'update'?

Dat laatste vind ik nogal omslachtig en ik hoop dus dat het eerste kan. Ik heb echter geen idee hoe...

Voorbeeldje nodig? Roep maar.

[ specs ] [ Tweaker gallery ]


Verwijderd

Geeft een query met update geen 0 ofzo als het veld niet bestaat, in dat geval kun je toch met een paar if-constructies enzo iets maken.

  • Explore
  • Registratie: Maart 2001
  • Laatst online: 08-04-2011

Explore

Op zoek naar werk

Topicstarter
Hm, dan kost 't me nog zeker twee queries in het geval dat het veld niet bestaat. En dus altijd nog een extra check. Kan het nog sneller?

[ specs ] [ Tweaker gallery ]


  • Rickets
  • Registratie: Augustus 2001
  • Niet online

Rickets

Finger and a shift

Je zou in de systeemtabellen kunnen kijken of het veld bestaat :)

If some cunt can fuck something up, that cunt will pick the worst possible time to fucking fuck it up, because that cunt’s a cunt.


Verwijderd

PHP:
1
2
3
4
5
6
$query = "Update tabel set blaat='blaat' where blaat='blaat'";
$result = mysql_query($query);
if ($result == "false"){
 $query = "Insert into table (blaat) values ('blaat')";
 mysql_query($query);
}


zoiets zou niet gaan ?

  • mr_obb
  • Registratie: Juni 2001
  • Laatst online: 21-08 12:59

mr_obb

Lakse Perfectionist

Hier iemand met exact hetzelfde probleem en een aantal reacties:

http://www.experts-exchan...ses/Mysql/Q_20541017.html

  • ThaDaNo
  • Registratie: Mei 2002
  • Laatst online: 05-04-2023
INSERT .... ON DUPLICATE KEY UPDATE

bedoel je dat?

is alleen niet met alle mysql versies compatible; maar met welke weet de handleiding vast wel

  • mr_obb
  • Registratie: Juni 2001
  • Laatst online: 21-08 12:59

mr_obb

Lakse Perfectionist

DaNo2002 schreef op 11 May 2003 @ 20:40:
INSERT .... ON DUPLICATE KEY UPDATE

bedoel je dat?

is alleen niet met alle mysql versies compatible; maar met welke weet de handleiding vast wel
Dat werkt vanaf versie 4.1, maar de exacte werking is mij niet helemaal duidelijk:
If you specify ON DUPLICATE KEY UPDATE clause (new in MySQL 4.1.0), and a row is
inserted that would cause a duplicate value in a PRIMARY or UNIQUE key, an UPDATE of
the old row is performed. For example, the command:
mysql> INSERT INTO table (a,b,c) VALUES (1,2,3)
--> ON DUPLICATE KEY UPDATE c=c+1;
in case of column a is declared as UNIQUE and already holds 1 once, would be identical
to the
mysql> UPDATE table SET c=c+1 WHERE a=1;
Note: that if column b is unique too, the UPDATE command would be written as
mysql> UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;
and if a=1 OR b=2 matches several rows, only one row will be updated! In general,
one should try to avoid using ON DUPLICATE KEY clause on tables with multiple UNIQUE
keys.
When one uses ON DUPLICATE KEY UPDATE, the DELAYED option is ignored.

  • Explore
  • Registratie: Maart 2001
  • Laatst online: 08-04-2011

Explore

Op zoek naar werk

Topicstarter
Dat laatste, 'on duplicate key update' zou wel eens kunnen zijn wat ik zoek. Alleen de server waarop m'n script komt te draaien heeft geen MySQL versie 4.1.0, maar 3.23.54, dus dat zal niet gaan. Doh!

Ik ben er compleet niet in thuis, maar kan PostgreSQL dit wel?

[ specs ] [ Tweaker gallery ]


  • bigtree
  • Registratie: Oktober 2000
  • Laatst online: 07-07 11:51
Explore schreef op 11 mei 2003 @ 20:18:
Is het met MySQL (en PHP) mogelijk om in 1 query een record toe te voegen, als een bepaald veld nog niet bestaat en te updaten als deze wel bestaat?
Als het om de primary key gaat, kan dat met replace

Lekker woordenboek, als je niet eens weet dat vandalen met een 'n' is.

Pagina: 1