gpt4 book ai didi

mysql - timestampdiff() 参数错误

转载 作者:行者123 更新时间:2023-11-29 10:48:21 25 4
gpt4 key购买 nike

我在sql fiddle上使用mysql版本5.6,并且我尝试使用timestampdiff()函数来查找名为Task的表中第一列中的最小值和第二列中的最大值之间的差异正在关注

select TimeStampDiff(month, , max(Task.End_Date), min(Task.Start_Date));

但是当我运行此代码时,出现以下错误:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“months(max(End_Date)),months(min(Start_Date)))”附近使用的正确语法

时间戳diff()是否不接受聚合函数作为参数?我该如何解决这个问题?

这是我的complete fiddle

最佳答案

尝试:

mysql> DROP TABLE IF EXISTS `Task`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `Task` (
-> `ID` INT NOT NULL,
-> `Pro_ID` INT NOT NULL,
-> `Start_Date` DATE,
-> `End_Date` DATE,
-> `Description` VARCHAR(255),
-> PRIMARY KEY (`ID`)
-> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `Task` VALUES
-> (1, 1, '2017-01-01', '2017-02-01', "no-Description-yet"),
-> (2, 1, '2017-01-01', '2017-02-01', "no-Description-yet"),
-> (3, 1, '2017-01-01', '2017-06-01', "no-Description-yet"),
-> (4, 2, '2017-01-01', '2017-03-01', "no-Description-yet"),
-> (5, 3, '2017-01-01', '2017-02-01', "no-Description-yet"),
-> (6, 4, '2017-01-01', '2017-03-01', "no-Description-yet");
Query OK, 6 rows affected (0.02 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> SELECT
-> `Pro_ID`,
-> TIMESTAMPDIFF(MONTH, MIN(`Start_Date`), MAX(`End_Date`)) `MONTH_DIFF`
-> FROM `Task`
-> GROUP BY `Pro_ID`;
+--------+------------+
| Pro_ID | MONTH_DIFF |
+--------+------------+
| 1 | 5 |
| 2 | 2 |
| 3 | 1 |
| 4 | 2 |
+--------+------------+
4 rows in set (0.00 sec)

示例 db-fiddle .

关于mysql - timestampdiff() 参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44176647/

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