My understanding of your question is that for each userid you have two entries, but you want to extract only one random case.
To achieve this, you must create a random value from 0 to 1 for each unique userid , and then CONNECT this list with the start list:
SELECT a.* FROM tbl_message_queue AS a JOIN ( SELECT userid, FLOOR(2*RAND()) AS type FROM tbl_message_queue GROUP BY userid ) AS b ON ( a.userid = b.userid AND a.type = b.type );
But if ORDER BY RAND() does not work for you, perhaps we should compromise.
In the above sequence, any two user identifiers will be uncorrelated - that is, the fact that user A gets type 0 says nothing about what user B will be added to.
Depending on the use case, a less random (but "seemingly random") sequence can be obtained by two queries:
SELECT @X := FLOOR(2*RAND()), @Y := POW(2,FLOOR(2+14*RAND()))-1; SELECT * FROM tbl_message_queue WHERE (((userid % @Y) & 1) XOR type XOR @X);
That way, you can get what seems like random extraction. What really happens is that the user IDs are correlated, and you only have a few dozen different extractions. But, using only simple operators and without JOIN, this query is very fast.
source share