ORDER BY random() with seed in SQLITE

前端 未结 4 807
别跟我提以往
别跟我提以往 2020-12-11 00:19

I would like to implement paging for a random set

Select * from Animals ORDER BY random(SEED) LIMIT 100 OFFSET 50  

I tried to set int to

4条回答
  •  伪装坚强ぢ
    2020-12-11 01:16

    I don't know if you're wanting a PHP and iOS solution, but if you are only interested in iOS and dont care much about using the built-in sqlite random() function, you could declare a custom function to use in your queries, one that does take a seed parameter.

    sqlite3_create_function(database, "CUSTOM_RANDOM", 1, SQLITE_UTF8, NULL, &CustomRandomSQLite, NULL, NULL);
    

    .

    void CustomRandomSQLite(sqlite3_context* context, int argc, sqlite3_value** argv)
    {
        if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_INTEGER)
        {
            const int seed = sqlite3_value_int(argv[0]);
            const int result = ...;
    
            sqlite3_result_int(context, result);
        }
        else
        {
            sqlite3_result_error(context, "Invalid", 0);
        }
    }
    

    .

    Select * from Animals ORDER BY CUSTOM_RANDOM(SEED) LIMIT 100 OFFSET 50
    

提交回复
热议问题