在mysql中限制要加入的行数 [英] Limit the number of rows to join to, in mysql

查看:92
本文介绍了在mysql中限制要加入的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想将两个表连接在一起,但是对于第一个表中的每一行,我只想将其连接到另一个表中前8个匹配的行,并按该表中的列之一排序.我可以使用任何巧妙的语法,还是需要弄乱子查询?

So I want to join two tables together, but for each row in the first table, I only want to join it to the top 8 matching rows in the other table, ordered by one of the columns in that table. Any clever syntax I can use, or do I need to get messy with subqueries?

推荐答案

看看

部分从每个组中选择前N行

这是一个较难解决的问题 解决.从每个查找单个行 借助SQL的汇总即可轻松实现分组 函数(MIN(),MAX()等). 从每个查找前几个 该方法无法进行分组 因为仅聚合函数 返回单个值.不过,这是 可以做到的.

This is a slightly harder problem to solve. Finding a single row from each group is easy with SQL’s aggregate functions (MIN(), MAX(), and so on). Finding the first several from each group is not possible with that method because aggregate functions only return a single value. Still, it’s possible to do.

这篇关于在mysql中限制要加入的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆