Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand" rel="nofollow">Order by Rand()</a> function to get a random record from the database.</p> <p><code>SELECT * FROM tbl_name ORDER BY RAND();</code></p> <p>You can also limit this to two rows easily enough as such:</p> <p><code>SELECT * FROM tbl_name ORDER BY RAND() limit 2;</code></p> <p>This will however get two random rows from the db, not two consecutive rows.</p> <p>If you wanted to get two random AND consecutive rows, you could run a query to get the ID and a subquery to get the next ID after it.</p> <p>As for keep data in one/two tables, definately keep it in two tables. Link your second table that keeps the votes with a link to the ID of the image. Obviously add indexes as required on the linked (and or other fields) as needed.</p> <p>Edit: Here is the code to get you the two subsequent rows of data in one row then:</p> <pre><code>select a.id, ( select min(c.id) from table1 c where c.id&gt;a.id limit 1 ) as id2 from table1 a order by rand() limit 1; </code></pre> <p>Edit 2: If you wanted them as separate rows to pull in all sorts of other bits, I have provided the query below. I used an extra subquery as if the initial query happened to pull out the max value (possible when using order by rand() then it solves the issue of only returning one row of data.</p> <pre><code>select b.id from table1 b, ( select a.id as id from table1 a where a.id&lt;(select max(id) from table1 limit 1) order by rand() limit 1 ) a where b.id&gt;=a.id order by b.id limit 2 ; </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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