dbTalk Databases Forums  

Select & Update in one query?

comp.databases.mysql comp.databases.mysql


Discuss Select & Update in one query? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jwcarlton
 
Posts: n/a

Default Select & Update in one query? - 09-23-2011 , 11:09 PM






I'm doing something like this:

SELECT id, title, text FROM table ORDER BY rand() LIMIT 3;

Then I run some PHP, extract IDs from array, and build an update query
like this:

UPDATE table SET selected=selected+1 WHERE id=1234 OR id=4321 OR
id=5678;

My question is, is there a way to do both the SELECT and UPDATE from a
single query, instead of using all of the PHP to get the IDs and then
running a second query?

TIA,

Jason

Reply With Quote
  #2  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Select & Update in one query? - 09-24-2011 , 03:23 AM






jwcarlton wrote:
Quote:
I'm doing something like this:

SELECT id, title, text FROM table ORDER BY rand() LIMIT 3;

Then I run some PHP, extract IDs from array, and build an update query
like this:

UPDATE table SET selected=selected+1 WHERE id=1234 OR id=4321 OR
id=5678;

My question is, is there a way to do both the SELECT and UPDATE from a
single query, instead of using all of the PHP to get the IDs and then
running a second query?

yes. :-)

UPDATE table SET selected=selected+1 WHERE id in (SELECT id, title, text
FROM table ORDER BY rand() LIMIT 3);

At a wild improbable guess..;-)


Quote:
TIA,

Jason

Reply With Quote
  #3  
Old   
jwcarlton
 
Posts: n/a

Default Re: Select & Update in one query? - 09-26-2011 , 04:37 PM



Quote:
I'm doing something like this:

SELECT id, title, text FROM table ORDER BY rand() LIMIT 3;

Then I run some PHP, extract IDs from array, and build an update query
like this:

UPDATE table SET selected=selected+1 WHERE id=1234 OR id=4321 OR
id=5678;

My question is, is there a way to do both the SELECT and UPDATE from a
single query, instead of using all of the PHP to get the IDs and then
running a second query?

yes. :-)

UPDATE table SET selected=selected+1 WHERE id in (SELECT id, title, text
FROM table ORDER BY rand() LIMIT 3);

At a wild improbable guess..;-)
Would I then be able to access the SELECTed fields in PHP?

Reply With Quote
  #4  
Old   
onedbguru
 
Posts: n/a

Default Re: Select & Update in one query? - 09-27-2011 , 08:36 PM



On Sep 26, 5:37*pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:
Quote:
I'm doing something like this:

SELECT id, title, text FROM table ORDER BY rand() LIMIT 3;

Then I run some PHP, extract IDs from array, and build an update query
like this:

UPDATE table SET selected=selected+1 WHERE id=1234 OR id=4321 OR
id=5678;

My question is, is there a way to do both the SELECT and UPDATE from a
single query, instead of using all of the PHP to get the IDs and then
running a second query?

yes. :-)

UPDATE table SET selected=selected+1 WHERE id in (SELECT id, title, text
FROM table ORDER BY rand() LIMIT 3);

At a wild improbable guess..;-)

Would I then be able to access the SELECTed fields in PHP?
No.

Reply With Quote
  #5  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Select & Update in one query? - 09-28-2011 , 03:06 AM



onedbguru wrote:
Quote:
On Sep 26, 5:37 pm, jwcarlton <jwcarl... (AT) gmail (DOT) com> wrote:
I'm doing something like this:
SELECT id, title, text FROM table ORDER BY rand() LIMIT 3;
Then I run some PHP, extract IDs from array, and build an update query
like this:
UPDATE table SET selected=selected+1 WHERE id=1234 OR id=4321 OR
id=5678;
My question is, is there a way to do both the SELECT and UPDATE from a
single query, instead of using all of the PHP to get the IDs and then
running a second query?
yes. :-)
UPDATE table SET selected=selected+1 WHERE id in (SELECT id, title, text
FROM table ORDER BY rand() LIMIT 3);
At a wild improbable guess..;-)
Would I then be able to access the SELECTed fields in PHP?

No.
UPDATE returns no row data.

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.