Skip to content
Advertisement

How to select one random id from the child table?

I am using 10.4.13-MariaDB. I have a parent table and a child table.

Parent table (tasks)

In interface it looks like

enter image description here

And I have a child table (puzzles) – puzzles table – puzzles can belong only to child themes

I want to get ONE random PUZZLE from EACH LEVEL. How to write a proper query?

Expected result:

My fiddle: https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=7bed2a19a0f98abccbe06ba9e0ae358b

Advertisement

Answer

With ROW_NUMBER() window function:

See the demo.

Partition of row_number() should be by the column parent_id of tasks, so the result has 1 row for each theme.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement