获取最近 3 组的所有行
我用谷歌搜索了一下,但没有找到任何对我有帮助的东西。
我有一个有效的 MySQL 查询,它选择一些列(跨三个表,带有两个 JOIN 语句),我希望对结果集做一些额外的事情。
我想从最近的 3 个组中选择所有行。 (我只能假设我必须在该列上使用 GROUP BY)我很难清楚地解释这一点,所以我将使用一个例子:
id | group
--------------
1 | 1
2 | 2
3 | 2
4 | 2
5 | 3
6 | 3
7 | 4
8 | 4
当然,为了简单起见,我把它简化了很多(并且我当前的查询不包含 id 列)。
现在我的理想查询将按顺序返回(即 id 字段):
8, 7, 6, 5, 4, 3, 2
如果我要添加以下第 9 个元素:
id | group
--------------
9 | 5
我的理想查询将按顺序返回:
9, 8, 7, 6, 5
因为这些都是最近 3 个组中的所有行。另外,当两行具有相同的组(并且仍在结果集中)时,我想按另一个字段对它们进行排序(我没有将其包含在我的简化示例中)。
在我的搜索中,我只找到了如何对 GROUPS 的元素(每个组元素的最大值、组元素的平均值等)执行操作,而不是对 GROUPS 本身(按字段排序的前 3 个组)执行操作。
预先感谢您的帮助!
编辑:这是我真正的查询的样子。
SELECT t1.f1, t1.f2, t2.f1, t2.f2, t2.f3, t3.f1, t3.f2, t3.f3, t3.f4
FROM t1
LEFT JOIN t2 ON t2.f1=t1.f3
LEFT JOIN t3 ON t2.f1=t3.f5
WHERE t1.f4='some_constant' AND t2.f4='some_other_constant'
ORDER BY t1.f2 DESC
I googled a bit and looked on SO but I didn't find anything that helped me.
I have a working MySQL query that selects some columns (accross three tables, with two JOIN statements) and I am looking to do something extra on the result set.
I would like to SELECT all rows from the 3 most recent groups. (I can only assume I have to use a GROUP BY on that column) I'm having a hard time explaining this clearly so I'll use an example:
id | group
--------------
1 | 1
2 | 2
3 | 2
4 | 2
5 | 3
6 | 3
7 | 4
8 | 4
Of course, I dumbed it down a lot for the sake of simplicity (and my current query doesn't include an id column).
Right now my ideal query would return, in order (that's the id field):
8, 7, 6, 5, 4, 3, 2
If I were to add the following 9th element:
id | group
--------------
9 | 5
My ideal query would then return, in order:
9, 8, 7, 6, 5
Because these are all the rows from the most 3 recent groups. Also, when two rows have the same group (and are still in the results set), I would like to ORDER them BY another field (which I have not included in my dumbed down example).
In my search I only found how to do actions on elements of GROUPS (MAX of each, AVG of group elements, etc.) and not GROUPS themselves (first 3 groups ordered by a field).
Thank you in advance for your help!
Edit: Here is what my real query looks like.
SELECT t1.f1, t1.f2, t2.f1, t2.f2, t2.f3, t3.f1, t3.f2, t3.f3, t3.f4
FROM t1
LEFT JOIN t2 ON t2.f1=t1.f3
LEFT JOIN t3 ON t2.f1=t3.f5
WHERE t1.f4='some_constant' AND t2.f4='some_other_constant'
ORDER BY t1.f2 DESC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
子查询应返回具有最大值的三个组,
INNER JOIN
将确保不包含没有这些组值的行。假设 t1.f2 是您的组列:
请注意,根据您的表结构,这可能非常低效,但这是我在没有更多信息的情况下可以做的最好的事情。
the subquery should return the three groups with the largest value, the
INNER JOIN
will ensure no rows are included which do not have these group values.assuming t1.f2 is your group column:
Note that this may be very inefficient depending on your table structure, but is the best I can do without more information.
这样怎么样...(我使用 groupId 而不是“group”
,最后你会得到:
how about this way... (i use groupId instead of 'group'
and finally you will have: