刘业锋
刘业锋

Reputation: 43

Is there any way to group and random some data in one sql?

I have a table with id from 1 to 10, now I need to random some data from 1 to 2, 3 to 5 and 6 to 10, such as random select 1,4,9, is there any way using one sql to resolve it?

table with id cloumn pic

Upvotes: 2

Views: 42

Answers (1)

Akina
Akina

Reputation: 42641

SELECT tablename.*
FROM tablename
JOIN ( SELECT ROUND(1 * RAND() + 1) random UNION ALL
       SELECT ROUND(2 * RAND() + 3) UNION ALL
       SELECT ROUND(4 * RAND() + 6) ) randoms ON tablename.id = randoms.random

Upvotes: 1

Related Questions