Answer the question
In order to leave comments, you need to log in
How to randomly select rows if there are duplicates in a certain field?
Good day!
Faced a problem of implementation of request of such sampling.
There is a table consisting of three fields ( id, header_id, user_id ). The value of the user_id field can be repeated multiple times. It is necessary to select rows within a certain range (i.e. ... WHERE user_id IN (...) ). In this case, if the value of user_id is repeated in the found rows, then one (row) of them must be randomly selected.
How can such a thing be implemented?
Thanks in advance!
Answer the question
In order to leave comments, you need to log in
SELECT
s.*
FROM
(SELECT
t.id
,t.header_id
,t.user_id
FROM
test t
WHERE
t.user_id in (1,2,3,4,5)
ORDER BY RAND()) s
GROUP BY s.user_id
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question