Unix Technical Forum

Is it possible to combine a SELECT and UPDATE in a single query

This is a discussion on Is it possible to combine a SELECT and UPDATE in a single query within the MySQL forums, part of the Database Server Software category; --> Is it possible to combine both of these queries into a single one so a connection is reserved immediately ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:28 AM
qwertycat@googlemail.com
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:28 AM
ZeldorBlat
 
Posts: n/a
Default 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>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:28 AM
Martijn Tonies
 
Posts: n/a
Default 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:28 AM
qwertycat@googlemail.com
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:28 AM
Martijn Tonies
 
Posts: n/a
Default 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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:28 AM
Paul Lautman
 
Posts: n/a
Default 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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:18 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com