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,410
  • 3
  • 12
  • 31

2 Answers2

2

TRY This

SELECT * 
FROM   employee 
WHERE  employeestatus != 2 
ORDER BY RANDOM()
LIMIT 1
Leigh
  • 28,765
  • 10
  • 55
  • 103
Reshil
  • 471
  • 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
  • 21,516
  • 3
  • 39
  • 51