简体   繁体   中英

update mysql table with random value from another query?

im having a problem regrading a query which will update earning table with another random value from users table. when executing the query it do nothing

$update = mysql_query("UPDATE earnings SET userid = (SELECT ID FROM users WHERE installid is NULL ORDER BY rand()) WHERE userid='0'");

in the second query

SELECT ID FROM users WHERE installid is NULL ORDER BY rand()

it will get me a random userid where installid null

Have you tried that query in phpMyAdmin etc? Are you getting an error?

Have you tried:

SELECT ID FROM users WHERE installid is NULL ORDER BY rand() LIMIT 1

So that the full query becomes:

UPDATE earnings SET userid = (SELECT ID FROM users WHERE installid is NULL ORDER BY rand() LIMIT 1) WHERE userid='0'

However keep in mind that you may end up having duplicate userid values in the earnings table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM