错误1064(42000)数据库错误,过度分割依据

问题描述 投票:1回答:1
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

还可以在DB中创建最多毫秒而不是秒的时间戳吗?我正在使用CURRENT_TIMESTAMP。

mysql sql window-functions mysql-5.7
1个回答
0
投票

Window函数(例如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-在您显示的样本数据中情况并非如此,因为所有时间戳都相同(我认为这是您的示例数据中的错字)。

© www.soinside.com 2019 - 2024. All rights reserved.