![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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..;-) |
#4
| |||
| |||
|
|
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? |
#5
| |||
| |||
|
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |