PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Update but insert if not exist
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Update but insert if not exist

Réponse
 
LinkBack Outils de la discussion
Vieux 16/12/2007, 16h48   #1
Steffan A. Cline
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Update but insert if not exist

I am trying to think of a trick way to handle something. I have been
successful in using the multiple inserts in one row by using the
,(xx,xx,xx),(xx,xx,xx),(xx,xx,xx) in a values block. BUT Is it possible to
insert if not there for example


Update xxxx
if anyone not found then insert new with same criteria as update
Where region_id in (2,3,4,5,6)



Thanks

Steffan

---------------------------------------------------------------
T E L 6 0 2 . 7 9 3 . 0 0 1 4 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
Steffan A. Cline
Steffan@ExecuChoice.net Phoenix, Az
http://www.ExecuChoice.net USA
AIM : SteffanC ICQ : 57234309
YAHOO : Steffan_Cline MSN : steffan@hldns.com
GOOGLE: Steffan.Cline Lasso Partner Alliance Member
---------------------------------------------------------------



  Réponse avec citation
Vieux 16/12/2007, 17h29   #2
Afan Pasalic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Update but insert if not exist

try REPLACE
http://dev.mysql.com/doc/refman/5.1/en/replace.html

-afan

Steffan A. Cline wrote:
> I am trying to think of a trick way to handle something. I have been
> successful in using the multiple inserts in one row by using the
> ,(xx,xx,xx),(xx,xx,xx),(xx,xx,xx) in a values block. BUT Is it possible to
> insert if not there for example
>
>
> Update xxxx
> if anyone not found then insert new with same criteria as update
> Where region_id in (2,3,4,5,6)
>
>
>
> Thanks
>
> Steffan
>
> ---------------------------------------------------------------
> T E L 6 0 2 . 7 9 3 . 0 0 1 4 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
> Steffan A. Cline
> Steffan@ExecuChoice.net Phoenix, Az
> http://www.ExecuChoice.net USA
> AIM : SteffanC ICQ : 57234309
> YAHOO : Steffan_Cline MSN : steffan@hldns.com
> GOOGLE: Steffan.Cline Lasso Partner Alliance Member
> ---------------------------------------------------------------
>
>
>
>

  Réponse avec citation
Vieux 17/12/2007, 15h55   #3
Rob Wultsch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Update but insert if not exist

On Dec 16, 2007 10:29 AM, Afan Pasalic <afan@afan.net> wrote:
> try REPLACE
> http://dev.mysql.com/doc/refman/5.1/en/replace.html
>
> -afan

Replace will not do an update. All previous data will be lost.

A mysql-centric alternative solution would be to use INSERT ... ON
DUPLICATE KEY UPDATE
http://dev.mysql.com/doc/refman/5.0/...duplicate.html
  Réponse avec citation
Vieux 17/12/2007, 17h21   #4
J Trahair
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Update but insert if not exist

This is a question I want to know the answer to, as well! Is there any way of avoiding looking up a specific record in a table to see if it exists, before deciding whether to INSERT INTO or UPDATE, eg:

mstrSQL = "SELECT * FROM Shops WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
Set rsRecordset = New ADODB.Recordset
gconn.CursorLocation = adUseServer
rsRecordset.Open mstrSQL, gconn, adOpenDynamic, adLockOptimistic
If rsRecordset.EOF = True Then
mstrSQL = "INSERT INTO Shops (ShopNameInFull, ShopReference, TillNumber) VALUES (strShopNameInFull, strShopReference, strTillNumber)"
mconn.Execute mstrSQL
Else
mstrSQL = "UPDATE Shops SET ShopNameInFull = 'strShopNameInFull', ShopReference = 'strShopReference', TillNumber = 'strTillNumber' WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
mconn.Execute mstrSQL
End If


.....just thought I'd ask!

Jonathan Trahair

  Réponse avec citation
Vieux 17/12/2007, 17h50   #5
Jay Pipes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Update but insert if not exist

INSERT ... ON DUPLICATE KEY UPDATE:

http://dev.mysql.com/doc/refman/5.0/...duplicate.html

Cheers,

Jay

J Trahair wrote:
> This is a question I want to know the answer to, as well! Is there any way of avoiding looking up a specific record in a table to see if it exists, before deciding whether to INSERT INTO or UPDATE, eg:
>
> mstrSQL = "SELECT * FROM Shops WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
> Set rsRecordset = New ADODB.Recordset
> gconn.CursorLocation = adUseServer
> rsRecordset.Open mstrSQL, gconn, adOpenDynamic, adLockOptimistic
> If rsRecordset.EOF = True Then
> mstrSQL = "INSERT INTO Shops (ShopNameInFull, ShopReference, TillNumber) VALUES (strShopNameInFull, strShopReference, strTillNumber)"
> mconn.Execute mstrSQL
> Else
> mstrSQL = "UPDATE Shops SET ShopNameInFull = 'strShopNameInFull', ShopReference = 'strShopReference', TillNumber = 'strTillNumber' WHERE ShopReference = '" & grd1.TextMatrix(numRowNo, 1) & "'"
> mconn.Execute mstrSQL
> End If
>
>
> ....just thought I'd ask!
>
> Jonathan Trahair
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 16h11.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11375 seconds with 13 queries