Using Limit random on mysql Using Limit random on mysql database database

Using Limit random on mysql


you can do this

update test3set display=0 where id in (select id from questions order by rand() limit 20);

Suppose you are using php.

$result = msyql_query("select id from questions order by rand() limit 20");$ids = array();while($row = msyql_fetch_assoc($result)){    $ids[] = $row['id'];  }

For your condition, first perform the first query and save the ids from the first query.

Suppose you are using php and you have saved the ids from the first query in $ids;

you can run the second query like this;

$idstring = implode(',',$ids);$query = 'update test3 set display=0 where id in ('.$idstring.' )';$result = mysql_query($query);


Use the WHERE clause to search a subquery

UPDATE test3SET display = 0WHERE id IN (SELECT id FROM questions ORDER BY RAND()LIMIT 20)

If you want to perform something between the SELECT and the UPDATE try this:

CREATE TABLE #Temp (    division TINYINT)INSERT INTO #Temp SELECT id FROM questions ORDER BY RAND()LIMIT 20--SELECT * FROM #TempUPDATE test3SET display = 0WHERE id IN (SELECT division FROM #Temp)