Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL: applying a random sort on multiple columns
    primarykey
    data
    text
    <p>In order to have a well scrambled table (for a psychological experiment), I'd like to sort each column of my array by RAND(). Althrough this code works:</p> <pre><code>SELECT Sort.Variable1, Sort.Variable2 FROM Sort ORDER BY Variable1, Variable2 ASC LIMIT 0 , 30 </code></pre> <p>replacing "ASC" by "RAND()" make the query fail. Can someone give me an advice (even a solution with PHP) ? </p> <p>Thanks</p> <p>Edit:</p> <p>Thanks to all your responses, I finally did it. Here's my PHP code for this (and sorry for the old-fashioned-not-brand-new-PDO-queries). Even if it's maybe useless, I post it:</p> <pre><code> $i=0; //Describe to retrieve variables' names $sqlCol= 'DESCRIBE Sort'; $sqlCol= mysql_query($sqlCol); while ($res=mysql_fetch_array($sqlCol)) { $var[$i]=$res['Field']; $i++; } $NbCol=mysql_num_rows($sqlCol); //Number of column to shuffle // Number of items for each column $sqlCount= 'SELECT COUNT(*) FROM Sort'; $req2= mysql_query($sqlCount) or die ('Err'); $NbLignes= mysql_result($req2,0,0) or die ();//Number of rows //Data array $sql= "SELECT * FROM Sort"; $req= mysql_query($sql) or die ('Err'); $sort=mysql_fetch_array($req); for($i=0;$i&lt;$NbCol;$i++) { ${'sql'.$i}='SELECT * FROM Sort ORDER BY RAND()'; ${'input'.$i} = mysql_query(${'sql'.$i}); while(${'array'.$i}=mysql_fetch_array(${'input'.$i})) { $bigArray[$i][]=${'array'.$i}[$i]; } } for($i=0;$i&lt;$NbLignes;$i++) { echo '&lt;div id="q'.$i.'"style="margin-bottom: 50px; float:left"&gt;Question '.($i+1); echo '&lt;ul id="sortable'.$i.'" class="sortable"&gt;'; for($j=0;$j&lt;$NbCol;$j++) { echo '&lt;li class="ui-state-default" id="'.$var[$j].$i.'" name="'.$var[$j].$i.'"&gt;'. $bigArray[$j][$i].'&lt;/li&gt;'; } echo '&lt;/ul&gt;&lt;/div&gt;'; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload