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 > update option to alter only 1 row?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
update option to alter only 1 row?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 02h00   #1
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut update option to alter only 1 row?


Is it possible to use some type of option in an update statement so that
only a single row (arbitrarily chosen) will get updated in the event that
there are two rows matching a where specification?

For example, in the following, if table W1 contains two rows where QN = 2,
then both are changed to the value 3. I just want 1 of them changed to 3 and
it doesn't matter which.

Update W1 set QN = 2 where QN = 3
Thanks







--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 06/02/2008, 08h01   #2
Andreas Stieger
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update option to alter only 1 row?

Hi,

Jeff wrote:

> I just want 1 of them changed to 3 and it doesn't matter which.


....LIMIT 1
  Réponse avec citation
Vieux 06/02/2008, 21h59   #3
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update option to alter only 1 row?


"Andreas Stieger" <Andreas.Stieger@gmx.de> wrote in message
news:47a96974$0$9109$9b4e6d93@newsspool2.arcor-online.net...
> Hi,
>
> Jeff wrote:
>
>> I just want 1 of them changed to 3 and it doesn't matter which.

>
> ...LIMIT 1


....still not working. The first query below does not work (mysql 5.22) with
the alter command, but the second does with select. Any ideas?


alter mytable set QN = 2 where QN = 3 limit 1; ** doesn't work **

select QN from mytable where QN = 3 limit 1; ** does work **




--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 07/02/2008, 01h23   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update option to alter only 1 row?

On Wed, 06 Feb 2008 22:59:16 +0100, Jeff <no_one@george.com> wrote:

>
> "Andreas Stieger" <Andreas.Stieger@gmx.de> wrote in message
> news:47a96974$0$9109$9b4e6d93@newsspool2.arcor-online.net...
>> Hi,
>>
>> Jeff wrote:
>>
>>> I just want 1 of them changed to 3 and it doesn't matter which.

>>
>> ...LIMIT 1

>
> ...still not working. The first query below does not work (mysql 5.22)
> with
> the alter command, but the second does with select. Any ideas?
>
>
> alter mytable set QN = 2 where QN = 3 limit 1; ** doesn't work **
>
> select QN from mytable where QN = 3 limit 1; ** does work **


Euhm, UPDATE statement, not ALTER....

UPDATE `mytable` SET `QN` = 2 WHERE `QN` = 3 LIMIT 1;
--
Rik Wasmus
  Réponse avec citation
Vieux 07/02/2008, 01h28   #5
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update option to alter only 1 row?

On Thu, 07 Feb 2008 02:23:27 +0100, Rik Wasmus
<luiheidsgoeroe@hotmail.com> wrote:

> On Wed, 06 Feb 2008 22:59:16 +0100, Jeff <no_one@george.com> wrote:
>
>>
>> "Andreas Stieger" <Andreas.Stieger@gmx.de> wrote in message
>> news:47a96974$0$9109$9b4e6d93@newsspool2.arcor-online.net...
>>> Hi,
>>>
>>> Jeff wrote:
>>>
>>>> I just want 1 of them changed to 3 and it doesn't matter which.
>>>
>>> ...LIMIT 1

>>
>> ...still not working. The first query below does not work (mysql 5.22)
>> with
>> the alter command, but the second does with select. Any ideas?
>>
>>
>> alter mytable set QN = 2 where QN = 3 limit 1; ** doesn't work **
>>
>> select QN from mytable where QN = 3 limit 1; ** does work **

>
> Euhm, UPDATE statement, not ALTER....
>
> UPDATE `mytable` SET `QN` = 2 WHERE `QN` = 3 LIMIT 1;


Oh, and for a more random row to be altered:
UPDATE `mytable` SET `QN` = 2 WHERE `QN` = 3 ORDER BY RAND() LIMIT 1;

see: <http://dev.mysql.com/doc/refman/5.1/en/update.html>
--
Rik Wasmus
  Réponse avec citation
Vieux 07/02/2008, 02h40   #6
Jeff
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: update option to alter only 1 row?


"Rik Wasmus" <luiheidsgoeroe@hotmail.com> wrote in message
newsp.t54rhdgk5bnjuv@metallium.lan...
On Wed, 06 Feb 2008 22:59:16 +0100, Jeff <no_one@george.com> wrote:

>
> "Andreas Stieger" <Andreas.Stieger@gmx.de> wrote in message
> news:47a96974$0$9109$9b4e6d93@newsspool2.arcor-online.net...


Euhm, UPDATE statement, not ALTER....

UPDATE `mytable` SET `QN` = 2 WHERE `QN` = 3 LIMIT 1;
--
Rik Wasmus

....it's always some simple typo that screws me up. I was using "update" in
the original code and for some reason was typing "alter" when I was testing
the "limit" option - thinking that the only thing I changed was the latter
when I actually typed in the wrong main command.

Thanks

Jeff



--
Posted via a free Usenet account from http://www.teranews.com

  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 12h22.


É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,16423 seconds with 14 queries