Du kan sortere efter position, rand()
for at have en tilfældig rækkefølge inden for hver position
skillevæg. Brug derefter variabel til at opregne poster for position
partitioner:
SELECT q_id, position, qtn, level, rn
FROM (
SELECT q_id, position, qtn, level,
@rn := IF(@pos = position, @rn + 1,
IF(@pos := position, 1, 1)) AS rn
FROM (
SELECT *
FROM tbl_question
WHERE level = '1'
ORDER BY position, rand() ) a
CROSS JOIN (SELECT @rn := 0, @pos := 0) AS vars ) b
WHERE b.rn <= 2
ORDER BY position
Den ydre forespørgsel bruger @rn
at vælge kun to poster fra hver position
partition.