Based on your comments, the approach below may work for you. This does not answer your specific question, but probably fits your requirements.
I’m going to assume that your requirements cannot change (for example, present 6 possible choices to users). Honestly, I think this is a bit of a strange requirement, but it does some interesting SQL. :-)
Here's my approach: generate 10 random numbers. Filter everything already in the database. Present 6 of these random numbers to your user. Random identifier numbers have very good transaction cost properties compared to sequential identifier numbers, so this should scale very well if your application becomes popular.
SELECT temp.i FROM ( SELECT 18 AS i
SQL Blog Polling Time !!!
- Why
WHERE mytable.i IS NULL line “ WHERE mytable.i IS NULL ” filter conflicts? (Hint: how can mytable.i be null when it is the primary key?)
Here are some test data:
CREATE TABLE mytable (i BIGINT PRIMARY KEY) ; INSERT INTO mytable VALUES (88), (3), (192), (123456) ;
Run the query above and here is the result. Note that 88, 192, and 123456 have been filtered out, as they will interfere with test data.
+---------+ | i | +---------+ | 18 | | 42 | | 191 | | 193 | | 1000 | | 1092930 | +---------+
And how to generate these random numbers? Probably rand () * 9223372036854775807 will work. (Assuming you don't want negative numbers!)
source share