I would like to run a SQL statement that selects two random entries from my table with the same type (for every type). I have an ‘id’, ‘question’ and ‘type’ field. I found the following code from a previous post here on stack overflow:
SET @type = ''; SET @num = 1; SELECT id, type, question FROM ( SELECT id, type, question, @num := IF(@type = type, @num + 1, 1) AS row_number, @type := type AS dummy FROM Q ) AS x WHERE row_number <3;
This pretty much solves my problem, the only thing is that the two output entries need to be random and not only the two top id's, or whatever is ordered by, by default.