获取最近 3 组的所有行

发布于 2024-11-09 10:34:52 字数 982 浏览 0 评论 0原文

我用谷歌搜索了一下,但没有找到任何对我有帮助的东西。

我有一个有效的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挖鼻大婶 2024-11-16 10:34:52
SELECT `table`.* FROM
(SELECT DISTINCT `group`
FROM `table`
ORDER BY `group` DESC LIMIT 3) t1
INNER JOIN `table` ON `table`.`group` = t1.`group`

子查询应返回具有最大值的三个组,INNER JOIN 将确保不包含没有这些组值的行。


假设 t1.f2 是您的组列:

SELECT a,b,c,d,e,f,g,h,i
FROM
(
  SELECT t1.f1 as a, t1.f2 as b, t2.f1 as c, t2.f2 as d, t2.f3 as e, t3.f1 as f, t3.f2 as g, t3.f3 as h, t3.f4 as i
  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
) first_table
INNER JOIN
(
  SELECT DISTINCT `f2`
  FROM `t1`
  ORDER BY `f2` DESC LIMIT 3
) second_table
ON first_table.b = second_table.f2

请注意,根据您的表结构,这可能非常低效,但这是我在没有更多信息的情况下可以做的最好的事情。

SELECT `table`.* FROM
(SELECT DISTINCT `group`
FROM `table`
ORDER BY `group` DESC LIMIT 3) t1
INNER JOIN `table` ON `table`.`group` = t1.`group`

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:

SELECT a,b,c,d,e,f,g,h,i
FROM
(
  SELECT t1.f1 as a, t1.f2 as b, t2.f1 as c, t2.f2 as d, t2.f3 as e, t3.f1 as f, t3.f2 as g, t3.f3 as h, t3.f4 as i
  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
) first_table
INNER JOIN
(
  SELECT DISTINCT `f2`
  FROM `t1`
  ORDER BY `f2` DESC LIMIT 3
) second_table
ON first_table.b = second_table.f2

Note that this may be very inefficient depending on your table structure, but is the best I can do without more information.

月亮是我掰弯的 2024-11-16 10:34:52

这样怎么样...(我使用 groupId 而不是“group”

[QUERY] => something like (SELECT id, groupId from tables.....) (your query with 2 joins).

-- with this query you have the last thre groups.
[QUERY2] => SELECT distinct(groupId) as groupId FROM ([QUERY]) ORDER BY groupId DESC LIMIT 0,3

,最后你会得到:

SELECT id, groupId from tables----...... WHERE groupId in ([QUERY2]) order by groupId DESC, id DESC

how about this way... (i use groupId instead of 'group'

[QUERY] => something like (SELECT id, groupId from tables.....) (your query with 2 joins).

-- with this query you have the last thre groups.
[QUERY2] => SELECT distinct(groupId) as groupId FROM ([QUERY]) ORDER BY groupId DESC LIMIT 0,3

and finally you will have:

SELECT id, groupId from tables----...... WHERE groupId in ([QUERY2]) order by groupId DESC, id DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文