1

Is it possible to randomly select a record from the database excluding some records with particular status?

For eg,

For example, I have a table for storing employee details.

id    employeename employeestatus
 1    ab           1
 2    cd           1
 3    ef           2
 4    gh           1
 5    ij           1

What I want from the query is to fetch a single random record whose status is not 2. Is it possible to do so? The database I'm using is PostgreSQL 8.4.15.

harry
  • 1,250
  • 3
  • 11
  • 31

2 Answers2

2

TRY This

SELECT * 
FROM   employee 
WHERE  employeestatus != 2 
ORDER BY RANDOM()
LIMIT 1
Leigh
  • 28,605
  • 10
  • 52
  • 98
Reshil
  • 461
  • 3
  • 9
1

Try this other question on the same topic

Best way to select random rows PostgreSQL

It's tricker than you think (to do efficiently)

Community
  • 1
  • 1
Richard Huxton
  • 19,615
  • 3
  • 35
  • 46