harry
harry

Reputation: 1522

How to (or Can I) select a random value from the Postgresql database excluding some particular records?

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.

Upvotes: 1

Views: 115

Answers (2)

Reshil
Reshil

Reputation: 481

TRY This

SELECT * 
FROM   employee 
WHERE  employeestatus != 2 
ORDER BY RANDOM()
LIMIT 1

Upvotes: 2

Richard Huxton
Richard Huxton

Reputation: 22893

Try this other question on the same topic

Best way to select random rows PostgreSQL

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

Upvotes: 1

Related Questions