Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do that by:</p> <ol> <li>Inventing a mechanism that provides the next available id when you want to insert (e.g. a transaction involving reading and incrementing an integer column somewhere -- pay special attention to the transaction isolation level!)</li> <li>Using <code>UPDATE</code> to decrement all ids greater than the one you just deleted (again, with a transaction -- don't forget that foreign keys must be ON UPDATE CASCADE!)</li> </ol> <p>But it begs the question: <em>why do you want this? is it going to be worth the trouble?</em></p> <p>It's almost certain that you can achieve whatever your goal is without such witchery.</p> <p><strong>Update (to address comment):</strong></p> <p>To select a random number of rows, you can do e.g. in MySQL</p> <pre><code>SELECT id FROM banners ORDER BY RAND() LIMIT 5 </code></pre> <p>to select 5 random, guaranteed existing banner ids.</p> <p><em>A word of caution:</em> there are quite a few people who view <code>ORDER BY RAND()</code> as a bad performance hog. However, it is IMHO not quite right to put every case in the same basket. If the number of rows in the table is manageable (I would consider anything below 10K to be not that many) then <code>ORDER BY RAND()</code> provides a very nice and succint solution. Also, <a href="http://dev.mysql.com/doc/refman/5.5/en/mathematical-functions.html#function_rand" rel="nofollow">the documentation</a> itself suggests this approach:</p> <blockquote> <p>However, you can retrieve rows in random order like this:</p> <p><code>mysql&gt; SELECT * FROM tbl_name ORDER BY RAND();</code></p> <p>ORDER BY RAND() combined with LIMIT is useful for selecting a random sample from a set of rows:</p> <p>mysql> SELECT * FROM table1, table2 WHERE a=b AND c ORDER BY RAND() LIMIT 1000; </p> <p>RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.</p> </blockquote>
    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. 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