Consider the following table:
tweets
-----------------------
id tweet class
-----------------------
1 Foo bar baz! 2
2 Lorem ipsum 2
3 Foobar lorem 3
4 Activi set 1
5 Baz baz bar? 3
7 Dolor mez foo 3
8 Samet set bar 1
In reality, the table has 600,000 records, but as indicated in the example table, some ids have been deleted (so the highest id > 600,000). class can be either 1, 2 or 3.
I need a random selection 1200 tweets, 400 of each class. Is this possible to do with one query?
If you have a finite number of possible values for
class(3), then it is easily done with a set ofUNIONqueries each having their ownORDER BY RAND()andLIMIT 400:For the
ORDER BYto be applied to eachUNIONgroup, the group must be enclosed in().