1 queries executed, 0 success, 1 errors, 0 warnings
查詢:SELECT t.`name`, DATE_FORMAT(str_to_date('2015'), '%Y') as statisDate, ROUND(IFNULL(SUM(t.`amount`), 0), 3) AS ...
錯誤代碼: 1582
Incorrect parameter count in the call to native function 'str_to_date'
執行耗時 : 0 sec
傳送時間 : 0 sec
總耗時 : 0 sec
2. 錯誤原因
SELECT
t.`name`,
DATE_FORMAT(STR_TO_DATE('2015'), '%Y') AS statisDate,
ROUND(IFNULL(SUM(t.`amount`), 0), 3) AS amount
FROM
t_stu_info t
WHERE 1 = 1
AND DATE_FORMAT(t.statisDate, '%Y') = '2015'
UNION
ALL
SELECT
t.`name`,
DATE_FORMAT(STR_TO_DATE('2014'), '%Y') AS statisDate,
ROUND(IFNULL(SUM(t.`amount`), 0), 3) AS amount
FROM
t_stu_info t
WHERE 1 = 1
AND DATE_FORMAT(t.statisDate, '%Y') = '2014'
MySQL內置函數str_to_date的格式是:str_to_date(字符串,日期格式);
在這個查詢SQL中卻是str_to_date(字符串),導致報錯
3. 解決辦法
修改查詢SQL語句
SELECT
t.`name`,
DATE_FORMAT(STR_TO_DATE('2015','%Y'), '%Y') AS statisDate,
ROUND(IFNULL(SUM(t.`amount`), 0), 3) AS amount
FROM
t_stu_info t
WHERE 1 = 1
AND DATE_FORMAT(t.statisDate, '%Y') = '2015'
UNION
ALL
SELECT
t.`name`,
DATE_FORMAT(STR_TO_DATE('2014','%Y'), '%Y') AS statisDate,
ROUND(IFNULL(SUM(t.`amount`), 0), 3) AS amount
FROM
t_stu_info t
WHERE 1 = 1
AND DATE_FORMAT(t.statisDate, '%Y') = '2014'