1

I am wondering if it is possible to get a random array of ID's from a table but include one in particular?

So say I have 200 rows, I might limit my script to output 20 but one of the rows must include id 2 (for example).

Not sure if this is possible, would appreciate any help received.

Andrew
  • 359
  • 2
  • 6
  • 20

2 Answers2

1
select id, if(id = 2, -1, rand()) as sort from my_table order by sort limit 20
Lee
  • 13,462
  • 1
  • 32
  • 45
  • Thank you this worked perfectly, I created an array with the results, shuffled the array and then used this in a seperate query to give me the results I needed :D – Andrew Sep 08 '13 at 12:00
0

Not the final solution, but maybe this thread helps you:

MySQL select 10 random rows from 600K rows fast

By the way: I'd handle the randomstuff within the script (e.g. PHP) with cached (e.g. Memcached) datasets. But that depends on your goal.

Community
  • 1
  • 1
Mr. B.
  • 8,041
  • 14
  • 67
  • 117