SQLite: How to achive RANDOM ORDER and pageintation at the same time?

走远了吗. 提交于 2020-04-16 05:15:12

问题


I have a table of movies, I want to be able to query the database and get a randomized list of movies, but also I don't want it to return all movies available so I'm using LIMIT and OFFSET. The problem is when I'm doing something like this:

SELECT *  FROM Movie ORDER BY RANDOM() LIMIT 50 OFFSET 0

and then when querying for the next page with LIMIT 50 OFFSET 50 the RANDOM seed changes and so it's possible for rows from the first page to be included in the second page, which is not the desired behavior.

How can I achieve a random order and preserve it through the pages? As far as I know SQLite doesn't support custom seed for it's RANDOM function.

Thank you!


回答1:


You cant preserve the random values. You have to add another field name to your table to keep the random order

UPDATE movie
SET randomOrder = Random();

Then you can retrive the pages

SELECT *  
FROM Movie 
ORDER BY randomOrder 
LIMIT 50 OFFSET 0


来源:https://stackoverflow.com/questions/44216165/sqlite-how-to-achive-random-order-and-pageintation-at-the-same-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!