2

My personel table id colum is 1,2,3, 4,6,7,8,9,12,13,14,15,16,20

How to get random 5 rows and id not in 1,2,6

My personel table has 100K records

2 Answers2

2

You could order the table by rand() and limit the results:

SELECT   id
FROM     personel
WHERE    id NOT IN (1, 2, 6)
ORDER BY rand()
LIMIT    5
Mureinik
  • 277,661
  • 50
  • 283
  • 320
0

You can filter out the ones you don't need using not in and order by rand() function to randomize the order, then limit rows to 5.

select * from table
where id not in (1,2,6)
order by rand() limit 5;
Gurwinder Singh
  • 37,207
  • 6
  • 50
  • 70