vendredi 23 octobre 2020

Sql query with mixed selected rows order

I have a table that looks like below:

| id | group_id | title |
-------------------------
| 1  | 1        | Hello |
| 2  | 1        | World |
| 3  | 2        | Foo   |
| 4  | 2        | Bar   |

My query may look like below to return the results above:

SELECT * FROM my_table ORDER BY id

Question

How can I order this table so that the group ids appears to be random, but still the same every time the query is executed.

Possible result example

This result looks to be in a random order. If I run the same query a week later, I want to see the exact same order which means it's not really random.

| id | group_id | title |
-------------------------
| 2  | 1        | World |
| 4  | 2        | Bar   |
| 1  | 1        | Hello |
| 3  | 2        | Foo   |
  • Appears to be random from a group_id perspective. It's no longer ordered by group_id like 1 1 2 2, but 1 2 1 2. It could also have been 2 1 1 2 or something that does not increase.
  • Should return the same results every time, not random each time.
  • I could order by title but if a title should change that row will be reordered. So the order needs to be made with the id I guess.
  • I want to avoid file or database caching if possible.

Is it possible?




Aucun commentaire:

Enregistrer un commentaire