I have some kind of impossible request :).
I have a table where one of the columns is named type
. I would like to SELECT 3 records for each type in that column. Is that possible?
Note also that I'm using MySQL and Sphinx.
UPDATE: Table structure
id title type
1 AAAA string1
2 CCCC string2
3 EEEE string2
4 DDDD string2
5 FFFF string2
6 BBBB string2
6 BBBB string2
What I want my MySQL to return is (up to 3 records for each type ordered by title):
id title type
1 AAAA string1
6 BBBB string2
2 CCCC string2
4 DDDD string2
Check out this article. Given:
Query:
(Uses a different article on the same site as Martin Wickman's answer!)
If you have an index on
(type, title)
, and you know the possible values fortype
, I believe that dynamic SQL is the way to go (for once) for best performance.For each possible value of
type
, add a union all and a select for that specific type. The final query will look like the following query:It executes in less than 1 second on a table with 1,000,000 rows, whereas the others solutions (Martins & Cyberkiwis) takes roughly 11 seconds.
The difference is because the unioned query above can fetch the first three title entries for each type and then stop, whereas the simulated analytics function has to scan the entire table.
When the table is large and collection is more unpredictable, the row numbering needs to be ordered by type in the inner query for the side-effecting variables to work.
Another way to do this without using side effecting variables, if no two records are exactly the same on (title, type, id), is given below. This uses only standard ANSI SQL92 SQL. It may be slower than the above though.