gpt4 book ai didi

mysql - 如何从有 300'000 行的表中选择 n 行

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:24 24 4
gpt4 key购买 nike

我试图从我的表中每 4 行取一次,但我的查询有一些错误。

这是我的 table :

id | IfInOctets |  IfOutOctets | inDiff | outDiff |    time    | timeDiff
---------------------------------------------------------------------------
1 | 283994207 | 117876089 | 1716 | 52872 | 1555658221 | 59
2 | 283995596 | 117928892 | 1389 | 52803 | 1555658282 | 61
3 | 283995978 | 117929215 | 382 | 323 | 1555658341 | 59
4 | 283996278 | 117929407 | 300 | 192 | 1555658402 | 61
5 | 283996595 | 117929703 | 317 | 296 | 1555658461 | 59
6 | 283998848 | 117932946 | 2253 | 3243 | 1555658522 | 61
7 | 284001482 | 117935214 | 2634 | 2268 | 1555658581 | 59
8 | 284001824 | 117935472 | 342 | 258 | 1555658642 | 61
9 | 284004728 | 117939762 | 2904 | 4290 | 1555658701 | 59

我的查询是:

SELECT t.id, t.time
FROM
(
SELECT id, `time`, ROW_NUMBER() OVER (ORDER BY `time`) AS rownum
FROM monitor
) AS t
WHERE t.rownum % 25 = 0
ORDER BY t.time

错误:

1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(ORDER BY `time`) AS rownum FROM mytable ) AS t WHERE t.rownu' at line 4

你可以在这里试试:

http://www.sqlfiddle.com/#!9/8074b4/3

最佳答案

您可以在 8.0 之前的 MySQL 中使用变量模拟 ROW_NUMBER() 函数:

SELECT t.id, t.time
FROM (SELECT id, time, @rownum := @rownum + 1 AS rownum
FROM monitor
CROSS JOIN (SELECT @rownum := 0) r
ORDER BY time) t
WHERE t.rownum % 4 = 0
ORDER BY t.time

输出(对于您的示例数据):

id  time
4 1555658402
8 1555658642

Demo on dbfiddle

关于mysql - 如何从有 300'000 行的表中选择 n 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55758315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com