SQL - 每种类型只给我 3 次点击
我有某种不可能的要求:)。
我有一个表,其中一列名为 type
。我想为该列中的每种类型选择 3 条记录。这可能吗?
另请注意,我使用的是 MySQL 和 Sphinx。
更新: 表结构
id title type
1 AAAA string1
2 CCCC string2
3 EEEE string2
4 DDDD string2
5 FFFF string2
6 BBBB string2
6 BBBB string2
我希望 MySQL 返回的是(按标题排序的每种类型最多 3 条记录):
id title type
1 AAAA string1
6 BBBB string2
2 CCCC string2
4 DDDD string2
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当表很大并且集合更加不可预测时,需要在内部查询中按类型对行编号进行排序,以使副作用变量发挥作用。
如果没有两条记录在(标题、类型、id)上完全相同,则另一种不使用副作用变量的方法如下所示。这仅使用标准 ANSI SQL92 SQL。但它可能比上面的慢。
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.
查看
查询:
Check out this article. Given:
Query:
(使用另一篇文章 与 Martin Wickman 的答案在同一网站上!)
(Uses a different article on the same site as Martin Wickman's answer!)
如果您在
(type, title)
上有索引,并且您知道type
的可能值,我相信动态 SQL 是(一次)用于最佳表现。对于
type
的每个可能值,为该特定类型添加 union all 和 select。最终查询将类似于以下查询:它在包含 1,000,000 行的表上执行不到 1 秒,而其他解决方案(Martins 和 Cyberkiwis)大约需要 11 秒。
区别在于,上面的联合查询可以获取每种类型的前三个标题条目,然后停止,而模拟分析函数必须扫描整个表。
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.