我在sql小提琴上使用mysql 5.6版,我正在尝试使用timestampdiff()函数来查找名为Task as的表中第一列中的最小值与第二列中的最大值之间的差异以下
select TimeStampDiff(month, , max(Task.End_Date), min(Task.Start_Date));
但是当我运行此代码时,我收到以下错误:
您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行的'月(最长(End_Date)),月(min(Start_Date)))附近使用正确的语法
是不是timestampdiff()不接受聚合函数作为参数?我该如何解决这个问题?
这是我的complete fiddle
答案 0 :(得分:1)
尝试:
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。