内连接两个表并列出特定行
假设我有两个表,如下所示:
+--------------+-------+
| ID | Score |
+--------------+-------
| 123 | 88 |
| 456 | 77 |
| 789 | 88 |
| 111 | 77 |
| 555 | 77 |
|444 | 88 |
| 222 | 77 |
| 333 | 88 |
第二个表:
+--------------+-----+
| ID |NAME | FScore |
+--------------+-------
| 123 |John | 106 |
| 456 |Aaron | 99 |
| 789 |Dan | 105 |
| 111 |Kevin | 200 |
| 555 |Tom | 100 |
| 444 |Jeff | 120 |
| 222 |Carl | 65 |
| 333 |Wayne | 101 |
我想连接两个表并根据 FScore 找出前 3 个 88
和前 3 个 77
行,例如:
+--------------+--------------+------+-----+
| ID | Score | NAME | FScore |
+--------------+--------------+------+-----+
| 444 | 88 | Jeff | 120 |
| 123 | 88 | John | 106 |
| 789 | 88 | Dan | 105 |
任何帮助很感激!
Let's say I have two tables as following:
+--------------+-------+
| ID | Score |
+--------------+-------
| 123 | 88 |
| 456 | 77 |
| 789 | 88 |
| 111 | 77 |
| 555 | 77 |
|444 | 88 |
| 222 | 77 |
| 333 | 88 |
SECOND TABLE:
+--------------+-----+
| ID |NAME | FScore |
+--------------+-------
| 123 |John | 106 |
| 456 |Aaron | 99 |
| 789 |Dan | 105 |
| 111 |Kevin | 200 |
| 555 |Tom | 100 |
| 444 |Jeff | 120 |
| 222 |Carl | 65 |
| 333 |Wayne | 101 |
I want to join two tables and based on FScore find out top 3 88
and top 3 77
rows like:
+--------------+--------------+------+-----+
| ID | Score | NAME | FScore |
+--------------+--------------+------+-----+
| 444 | 88 | Jeff | 120 |
| 123 | 88 | John | 106 |
| 789 | 88 | Dan | 105 |
Any helps are appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用返回连接表的 CTE,然后使用相关子查询过滤结果集:
或者使用
ROW_NUMBER()
窗口函数:请参阅 演示。
You can use a CTE that returns the joined tables and then filter the resultset with a correlated subquery:
Or, with
ROW_NUMBER()
window function:See the demo.