偏移MySQL Max

发布于 2024-10-12 04:02:56 字数 1158 浏览 4 评论 0 原文

我正在运行 MySQL 查询来获取按每个字段分组的每行的最高 ID。我这样做是:

SELECT period,max(id) AS maxid
FROM f
WHERE type = '1'
GROUP BY period

这会产生:

+--------+-------+
| period | maxid |
+--------+-------+
| 1      | 21878 |
| 2      | 21879 |
| 3      | 20188 |
| 4      | 21873 |
| 5      | 21872 |
| 6      | 21874 |
| 7      | 21875 |
| 8      | 21876 |
| 9      | 21877 |
+--------+-------+

这是我期待的结果。

但是,我现在想要运行一个查询,该查询返回最大 id,但每个周期返回一个。我认为最好的方法是使用 LIMIT 上的偏移参数。为了测试这是否有效,我运行了:

SELECT period,(SELECT id FROM freight_data ORDER BY id DESC LIMIT 1) AS maxid
FROM f
WHERE type = '1'
GROUP BY period

这会产生:

+--------+-------+
| period | maxid |
+--------+-------+
| 1      | 21903 |
| 2      | 21903 |
| 3      | 21903 |
| 4      | 21903 |
| 5      | 21903 |
| 6      | 21903 |
| 7      | 21903 |
| 8      | 21903 |
| 9      | 21903 |
+--------+-------+

我可以看到为什么会发生这种情况,因为我的子查询在获取 ID 时没有考虑任何条件,所以它只是返回表中的最高 ID 。

因此,我的问题是:

  • MAX 是如何工作的?有
  • 没有一种方法可以产生与 max(id) 类似的结果,但偏移一个结果?

任何帮助将不胜感激!

谢谢

I'm running a MySQL query to get the highest ID of each row grouped by each field. I do this with:

SELECT period,max(id) AS maxid
FROM f
WHERE type = '1'
GROUP BY period

This produces:

+--------+-------+
| period | maxid |
+--------+-------+
| 1      | 21878 |
| 2      | 21879 |
| 3      | 20188 |
| 4      | 21873 |
| 5      | 21872 |
| 6      | 21874 |
| 7      | 21875 |
| 8      | 21876 |
| 9      | 21877 |
+--------+-------+

This is the result I am expecting.

However, I now want to run a query which returns the maximum id but one for each period. I figured the best way to do this would be to use the offset paramater on LIMIT. To test that this will work, I ran:

SELECT period,(SELECT id FROM freight_data ORDER BY id DESC LIMIT 1) AS maxid
FROM f
WHERE type = '1'
GROUP BY period

This produces:

+--------+-------+
| period | maxid |
+--------+-------+
| 1      | 21903 |
| 2      | 21903 |
| 3      | 21903 |
| 4      | 21903 |
| 5      | 21903 |
| 6      | 21903 |
| 7      | 21903 |
| 8      | 21903 |
| 9      | 21903 |
+--------+-------+

I can see why this is happening, as my subquery isn't taking any of the conditions in to account when getting the ID, so it's just returning the highest ID in the table.

Thus, my questions are:

  • How does MAX work? and
  • Is there a way I can product a similar result as max(id) but offset by one result?

Any help will be greatly appreciated!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

北城半夏 2024-10-19 04:02:56

你可以这样做,这只是有点可怕:

SELECT DISTINCT ff.period, (
    SELECT id 
    FROM f 
    WHERE period = ff.period 
    AND type = '1' 
    ORDER BY id DESC
    LIMIT 1, 1
) as max_id_but_1
FROM f as ff
WHERE type = '1';

编辑:

如果每个 id 只属于一个时期,我认为你可以使用这个:

SELECT period, max(id)
FROM f
WHERE type = '1'
AND id NOT IN (
    SELECT max(id)
    FROM f
    WHERE type = '1'
    GROUP BY period
)
GROUP BY period;

但是,你不会得到只有一行的时期的结果。当然,您可以围绕它进行编码。

You could do this, which is only slightly horrible:

SELECT DISTINCT ff.period, (
    SELECT id 
    FROM f 
    WHERE period = ff.period 
    AND type = '1' 
    ORDER BY id DESC
    LIMIT 1, 1
) as max_id_but_1
FROM f as ff
WHERE type = '1';

EDIT:

If every id belongs to only one period, I think you can use this:

SELECT period, max(id)
FROM f
WHERE type = '1'
AND id NOT IN (
    SELECT max(id)
    FROM f
    WHERE type = '1'
    GROUP BY period
)
GROUP BY period;

However, you will not get results for periods with only one row. Of course, you could code around that.

转身以后 2024-10-19 04:02:56

如果我正确理解你的问题,你想要每个时期的第二高 ID,对吗?

这太棒了,当然没有经过测试:

SELECT period,max(id) AS maxid
FROM f
WHERE type = '1'
AND maxid NOT IN(
  SELECT period,max(id) AS maxid
  FROM f
  WHERE type = '1'
  GROUP BY period
)
GROUP BY period

您可能会在标识符“maxid”上遇到一些冲突。

If I understand your question correctly you want the second-highest id for each period, right?

This is ugh-tastic and not tested of course:

SELECT period,max(id) AS maxid
FROM f
WHERE type = '1'
AND maxid NOT IN(
  SELECT period,max(id) AS maxid
  FROM f
  WHERE type = '1'
  GROUP BY period
)
GROUP BY period

You might get some conflicts on the identifier 'maxid'.

泛泛之交 2024-10-19 04:02:56
SELECT  period,
        (
        SELECT  id
        FROM    f fi
        WHERE   fi.type = '1'
                AND fi.period = f.period
        ORDER BY
                type DESC, period DESC, id DESC
        LIMIT 1, 1
        )
FROM    f
WHERE   type = '1'
GROUP BY
        period

f(类型、句点、id) 上创建索引,以便快速运行。

SELECT  period,
        (
        SELECT  id
        FROM    f fi
        WHERE   fi.type = '1'
                AND fi.period = f.period
        ORDER BY
                type DESC, period DESC, id DESC
        LIMIT 1, 1
        )
FROM    f
WHERE   type = '1'
GROUP BY
        period

Create an index on f (type, period, id) for this to work fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文