PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Multiple updates
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Multiple updates

Réponse
 
LinkBack Outils de la discussion
Vieux 25/11/2008, 19h45   #1
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Multiple updates

Hello all,

I'm not very proficient with MySQL syntax...
I was trying to do the following elementary thing but can't figure out the
right syntax:

UPDATE ImageList SET Avg=1 WHERE Name=abc SET Avg=5 WHERE Name=erg SET
Avg=17 WHERE Name=gvd

I tried with "()," around each SET/WHERE, I also tried to repeat the UPDATE
to no avail.

Thanks for any pointers.
--
Guillaume Dargaud
http://www.gdargaud.net/


  Réponse avec citation
Vieux 25/11/2008, 20h01   #2
Dutchie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple updates

Guillaume Dargaud schreef:
> Hello all,
>
> I'm not very proficient with MySQL syntax...
> I was trying to do the following elementary thing but can't figure out the
> right syntax:
>
> UPDATE ImageList SET Avg=1 WHERE Name=abc SET Avg=5 WHERE Name=erg SET
> Avg=17 WHERE Name=gvd
>
> I tried with "()," around each SET/WHERE, I also tried to repeat the UPDATE
> to no avail.
>
> Thanks for any pointers.


Easiest is probably this:

UPDATE ImageList SET Avg=1 WHERE Name='abc';
UPDATE ImageList SET Avg=5 WHERE Name='erg';
UPDATE ImageList SET Avg=17 WHERE Name='gvd';
  Réponse avec citation
Vieux 25/11/2008, 20h40   #3
Guillaume Dargaud
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple updates

> Easiest is probably this:
>
> UPDATE ImageList SET Avg=1 WHERE Name='abc';
> UPDATE ImageList SET Avg=5 WHERE Name='erg';
> UPDATE ImageList SET Avg=17 WHERE Name='gvd';


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 '; UPDATE
ImageList SET AvgColor=7891816 WHERE Name='03'; UPDATE ImageList SET ' at
line 1

I'm doing this in php and use something like:
$query.="UPDATE ImageList SET Avg=$Avg WHERE Name='$Name'; \n";
(also tried without the \n)

Hmmm, what's wrong ?
--
Guillaume Dargaud
http://www.gdargaud.net/


  Réponse avec citation
Vieux 25/11/2008, 21h08   #4
Dutchie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple updates

Guillaume Dargaud schreef:
>> Easiest is probably this:
>>
>> UPDATE ImageList SET Avg=1 WHERE Name='abc';
>> UPDATE ImageList SET Avg=5 WHERE Name='erg';
>> UPDATE ImageList SET Avg=17 WHERE Name='gvd';

>
> 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 '; UPDATE
> ImageList SET AvgColor=7891816 WHERE Name='03'; UPDATE ImageList SET ' at
> line 1
>
> I'm doing this in php and use something like:
> $query.="UPDATE ImageList SET Avg=$Avg WHERE Name='$Name'; \n";
> (also tried without the \n)
>
> Hmmm, what's wrong ?


The query I gave you however is correct.
You can see this if you run it in a tool like phpMyAdmin.
You are not using PHP correctly.

Don't use \n in your queries.
The PHP function mysql_query does not support multiple queries.
I don't mean to be rude, but I advise you to Google for a PHP/MySQL
tutorial.
  Réponse avec citation
Vieux 26/11/2008, 13h39   #5
sheldonlg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple updates

Dutchie wrote:
> Guillaume Dargaud schreef:
>>> Easiest is probably this:
>>>
>>> UPDATE ImageList SET Avg=1 WHERE Name='abc';
>>> UPDATE ImageList SET Avg=5 WHERE Name='erg';
>>> UPDATE ImageList SET Avg=17 WHERE Name='gvd';

>>
>> 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 '; UPDATE ImageList SET AvgColor=7891816 WHERE Name='03'; UPDATE
>> ImageList SET ' at line 1
>>
>> I'm doing this in php and use something like:
>> $query.="UPDATE ImageList SET Avg=$Avg WHERE Name='$Name'; \n";
>> (also tried without the \n)
>>
>> Hmmm, what's wrong ?

>
> The query I gave you however is correct.
> You can see this if you run it in a tool like phpMyAdmin.
> You are not using PHP correctly.
>
> Don't use \n in your queries.
> The PHP function mysql_query does not support multiple queries.
> I don't mean to be rude, but I advise you to Google for a PHP/MySQL
> tutorial.


In addition to what Dutchie said, if all the queries need to be treated
as one, then change the table engine to innoDb and use a transaction.
You do that with:

mysql_query("BEGIN");
$result = mysql_query(the first one);
if ($result) $result = mysql_query(the second one);
if ($result) $result = mysql_query(the third one);
if ($result) mysql_query("COMMIT");
else mysql_query("ROLLBACK");
  Réponse avec citation
Vieux 06/12/2008, 04h18   #6
Michael Austin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple updates

Guillaume Dargaud wrote:
> Hello all,
>
> I'm not very proficient with MySQL syntax...
> I was trying to do the following elementary thing but can't figure out the
> right syntax:
>
> UPDATE ImageList SET Avg=1 WHERE Name=abc SET Avg=5 WHERE Name=erg SET
> Avg=17 WHERE Name=gvd
>
> I tried with "()," around each SET/WHERE, I also tried to repeat the UPDATE
> to no avail.
>
> Thanks for any pointers.



Since you asked for a MYSQL solution (not PHP or some other language)

update ImageList
SET Avg= CASE
WHEN Name='abc' THEN 1
WHEN Name='erg' THEN 5
WHEN Name='gvd' THEN 17
ELSE Name !!just in case your select went wild
END
WHERE Name in ('abc','erg','gvd');


Some things to know:
The ELSE statement and WHERE clauses are optional - but I like using
them to ensure that only the correct rows are updated.

Learn the ANSI Standards and in particular the CASE statement - it is
very powerful and transportable across most database platforms. While
MYSQL has a lot of unique functions (as does MSSqlServer, Oracle and
most of the others) if you learn ANSI Standards, it [generally] does not
matter which database you run them on... (Using concat(...) for example
is MYSQL only...) and you would need to make some modifications to your
code should you decide to move it to a different platform.
  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 02h09.


Édité par : vBulletin®
Copyright ©2000 - 2009, 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,12423 seconds with 14 queries