Home > front end >  Get random rows from sqlite database, then sort by column
Get random rows from sqlite database, then sort by column

Time:03-14

With each new query I want to obtain a random set of 10 rows from the table tasks. The rows should then be sorted by column difficulty.

I tried this but order by difficulty is ignored:

SELECT id, difficulty
FROM tasks
ORDER BY random(), difficulty
LIMIT 10

CodePudding user response:

First get the 10 random rows in a subquery and then sort them by difficulty:

SELECT *
FROM (
  SELECT id, difficulty
  FROM tasks
  ORDER BY random()
  LIMIT 10
)
ORDER BY difficulty;
  • Related