Skip to content

How to get random recrods from findMany? #5886

Answered by nikolasburk
hyochan asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @hyochan, thanks a lot for raising this! I don't think returning items from findMany in a random order is natively supported by Prisma Client yet and I don't think there's another way how you could retrieve a random element with Prisma Client. If you only care about getting a single item though and not really about the randomness, you can use findFirst.

Alternatively, you can of course use the SQL query that you posted above and send it to the DB using Prisma Client's $queryRaw method.

In any case, I think this is a really interesting feature request, would you mind persisting it in a GitHub issue so that our Product and Engineering teams can look into it?

Replies: 15 comments 23 replies

Comment options

You must be logged in to vote
2 replies
@hyochan
Comment options

@hyochan
Comment options

Answer selected by jharrell
Comment options

You must be logged in to vote
6 replies
@hlspablo
Comment options

@felinto-dev
Comment options

@BenjaminPiette
Comment options

@igor-ht
Comment options

@tuon1602
Comment options

Comment options

You must be logged in to vote
2 replies
@felinto-dev
Comment options

@AroldoGoulart
Comment options

Comment options

You must be logged in to vote
6 replies
@sawirricardo
Comment options

@jgb-solutions
Comment options

@jgb-solutions
Comment options

@danvayn
Comment options

@Maximilian94
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@shtse8
Comment options

@KevinBatdorf
Comment options

@cah4a

This comment was marked as disruptive content.

@lilonghe
Comment options

@SH20RAJ
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@jgb-solutions
Comment options

Comment options

You must be logged in to vote
1 reply
@stepandel
Comment options

Comment options

You must be logged in to vote
0 replies

This comment was marked as disruptive content.

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet