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 > Is it possible to combine a SELECT and UPDATE in a single query
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is it possible to combine a SELECT and UPDATE in a single query

Réponse
 
LinkBack Outils de la discussion
Vieux 27/09/2007, 22h55   #1 (permalink)
qwertycat@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Is it possible to combine a SELECT and UPDATE in a single query

Is it possible to combine both of these queries into a single one so a
connection is reserved immediately when found so a competing script
doesn't take up the connection in between

SELECT Server_ID, Server_Address, Server_Port WHERE
Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
LIMIT 1"

UPDATE Servers SET Server_Connections_In_Use =
Server_Connections_In_Use+1 WHERE Server_ID = '$Server_ID' LIMIT 1

  Réponse avec citation
Vieux 28/09/2007, 00h10   #2 (permalink)
ZeldorBlat
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to combine a SELECT and UPDATE in a single query

On Sep 27, 5:55 pm, qwerty...@googlemail.com wrote:
> Is it possible to combine both of these queries into a single one so a
> connection is reserved immediately when found so a competing script
> doesn't take up the connection in between
>
> SELECT Server_ID, Server_Address, Server_Port WHERE
> Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
> LIMIT 1"
>
> UPDATE Servers SET Server_Connections_In_Use =
> Server_Connections_In_Use+1 WHERE Server_ID = '$Server_ID' LIMIT 1


Use transactions:

<http://dev.mysql.com/doc/refman/5.0/en/commit.html>

  Réponse avec citation
Vieux 28/09/2007, 10h37   #3 (permalink)
Martijn Tonies
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to combine a SELECT and UPDATE in a single query

> Is it possible to combine both of these queries into a single one so a
> connection is reserved immediately when found so a competing script
> doesn't take up the connection in between
>
> SELECT Server_ID, Server_Address, Server_Port WHERE
> Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
> LIMIT 1"
>
> UPDATE Servers SET Server_Connections_In_Use =
> Server_Connections_In_Use+1 WHERE Server_ID = '$Server_ID' LIMIT 1


How about:

UPDATE Servers SET Server_Connections_In_Use =
Server_Connections_In_Use+1 WHERE Server_ID =
(SELECT Server_ID WHERE
Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
LIMIT 1)



--
Martijn Tonies
Database Workbench - development tool for MySQL, and more!
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com


  Réponse avec citation
Vieux 28/09/2007, 21h33   #4 (permalink)
qwertycat@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to combine a SELECT and UPDATE in a single query

On Sep 28, 10:37 am, "Martijn Tonies"
<m.ton...@upscene.removethis.com> wrote:
> How about:
>
> UPDATE Servers SET Server_Connections_In_Use =
> Server_Connections_In_Use+1 WHERE Server_ID =
> (SELECT Server_ID WHERE
> Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
> LIMIT 1)


Thanks but:

"There is one caveat: It is not currently possible to modify a table
and select from the same table in a subquery."
http://dev.mysql.com/tech-resources/...ubqueries.html

  Réponse avec citation
Vieux 28/09/2007, 23h20   #5 (permalink)
Martijn Tonies
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to combine a SELECT and UPDATE in a single query


> > How about:
> >
> > UPDATE Servers SET Server_Connections_In_Use =
> > Server_Connections_In_Use+1 WHERE Server_ID =
> > (SELECT Server_ID WHERE
> > Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
> > LIMIT 1)

>
> Thanks but:
>
> "There is one caveat: It is not currently possible to modify a table
> and select from the same table in a subquery."
> http://dev.mysql.com/tech-resources/...ubqueries.html


Oh, right. Sorry for that, I don't usually do MySQL SQL statements.

Silly restriction, really.


--
Martijn Tonies
Database Workbench - tool for InterBase, Firebird, MySQL, NexusDB, Oracle &
MS SQL Server
Upscene Productions
http://www.upscene.com
My thoughts:
http://blog.upscene.com/martijn/
Database development questions? Check the forum!
http://www.databasedevelopmentforum.com


  Réponse avec citation
Vieux 29/09/2007, 12h37   #6 (permalink)
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is it possible to combine a SELECT and UPDATE in a single query

qwertycat@googlemail.com wrote:
> On Sep 28, 10:37 am, "Martijn Tonies"
> <m.ton...@upscene.removethis.com> wrote:
>> How about:
>>
>> UPDATE Servers SET Server_Connections_In_Use =
>> Server_Connections_In_Use+1 WHERE Server_ID =
>> (SELECT Server_ID WHERE
>> Servers.Server_Connections_In_Use < Servers.Server_Max_Connections
>> LIMIT 1)

>
> Thanks but:
>
> "There is one caveat: It is not currently possible to modify a table
> and select from the same table in a subquery."
> http://dev.mysql.com/tech-resources/...ubqueries.html


I was going to sugget using a join instead, but you cannot use a LIMIT then.

Your first query doesn't have a FROM table clause.

Can you give us more information on what this is actually doing and supply
some real working queries.


  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 03h19.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12187 seconds with 14 queries