Search code examples
sqlpostgresqlsubquery

How to get random id from database using random_between


I try something like

SELECT * FROM table WHERE aspect_id = (SELECT floor(random(aspect_id)::int FROM generate_series(1,8));

and I want to get several aspect_id from table. Or I try to

SELECT * FROM table WHERE aspect_id = SELECT random_between(1,100) FROM generate_series(1,5);

But still nothing Could you help me?


Solution

  • What about

    SELECT * FROM atable
    WHERE aspect_id BETWEEN 1 AND 100
    ORDER BY random()
    LIMIT 5;