gpt4 book ai didi

mysql - ERROR 1064 (42000) over partition by 语法中的数据库错误

转载 作者:行者123 更新时间:2023-11-29 09:24:06 25 4
gpt4 key购买 nike

mysql> select * from FinalTable;
+------+-------+-------+---------------------+
| id | name | state | timestamp |
+------+-------+-------+---------------------+
| 12 | name1 | TX | 2020-01-25 11:29:36 |
| 14 | name3 | CA | 2020-01-25 11:29:36 |
| 14 | name3 | TX | 2020-01-25 11:29:36 |
| 12 | name1 | CA | 2020-01-25 11:29:36 |
| 13 | name2 | TA | 2020-01-25 11:29:36 |
| 14 | name3 | CA | 2020-01-25 11:29:36 |
+------+-------+-------+---------------------+

我正在查看输出查询,其响应如下:

I2 name1 TX 2020-01-25 11:29:36  CA 2020-01-25 11:29:36

当我运行查询时,

select id,name,state,timestamp,
lead(state,1) over (partition by id order by timestamp asc) out_state,
lead(timestamp,1) over (partition by id order by timestamp asc) out_timestamp
from FinalTable

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(partition by id order by timestamp asc) out_state,
lead(timestamp,1) over (part' at line 2

还可以在数据库中创建高达毫秒而不是秒的时间戳吗?我正在使用 CURRENT_TIMESTAMP。

最佳答案

窗口函数(例如lead())仅在MySQL 8.0中添加,因此它们在5.7版本中不可用。您可以使用自连接来模拟 lead() ,如下所示:

select t.*, tlead.state, tlead.timestamp
from FinalTable t
left join FinalTable tlead
on tlead .id = t.id
and tlead.timestamp = (
select min(t1.timestamp)
from FinalTable t1
where t1.id = t.id and t1.timestamp > t.timestamp
)

旁注:要使此方法正常工作,您需要相同 id 的后续记录具有不同的 timestamp - 示例数据中的情况并非如此您所展示的,其中所有时间戳都是相同的(我认为这是您的示例数据中的拼写错误)。

关于mysql - ERROR 1064 (42000) over partition by 语法中的数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59912125/

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