1

我生成了以下 SQL 代码:

DELIMITER $$

CREATE FUNCTION flighttime (p_arrival(DATE), p_departure(DATE))
RETURNS CHAR(50)
BEGIN
    DECLARE v_hour INT;
    DECLARE v_minute INT;
    DECLARE v_htext CHAR(10);
    DECLARE v_mtext CHAR(10);
    DECLARE v_flighttime CHAR(50);
    DATEDIFF(minute, p_departure, p_arrival) AS v_minute;
    v_hour = v_minute/60;
    v_minute = v_minute-(v_hour*60);

    IF v_hour<2 AND v_minute<2 THEN SET v_htext=' hour and ' AND v_mtext=' minute';
    ELSEIF v_hour<2 AND v_minute>1 THEN SET v_htext=' hour and ' AND v_mtext=' minutes';
    ELSEIF v_hour>1 AND v_minute<2 THEN SET v_htext=' hours and ' AND v_mtext=' minute';
    ELSE SET v_htext=' hours and ' AND v_mtext=' minutes';

    SET v_flighttime = CONCAT(v_hour, v_mtext, v_minute, v_mtext);
    RETURN v_flighttime;
END

$$
DELIMITER;

更正:

DELIMITER $$
CREATE FUNCTION flighttime (p_arrival DATETIME, p_departure DATETIME)
    RETURNS CHAR(70)
BEGIN
    DECLARE v_hour INT;
    DECLARE v_minute INT;
    DECLARE v_htext CHAR(15);
    DECLARE v_mtext CHAR(15);
    DECLARE v_flighttime CHAR(70);
    SET v_hour = HOUR(TIMEDIFF(p_departure, p_arrival));
    SET v_minute = MINUTE(TIMEDIFF(p_departure, p_arrival));
    IF v_hour<2 AND v_minute<2 THEN 
        SET v_htext = ' hour and ';
        SET v_mtext=' minute';
    ELSEIF v_hour<2 AND v_minute>1 THEN 
        SET v_htext=' hour and '; 
        SET v_mtext=' minutes';
    ELSEIF v_hour>1 AND v_minute<2 THEN 
        SET v_htext=' hours and ';
        SET v_mtext=' minute';
    ELSE 
        SET v_htext=' hours and ';
        SET v_mtext=' minutes';
    END IF;

    SET v_flighttime = CONCAT(v_hour, v_htext, v_minute, v_mtext);

    RETURN v_flighttime;
END $$
DELIMITER ;

但我在 phpMyAdmin 3.5.2.2 中收到以下错误消息:

1064 - 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 '(DATE), p_departure(DATE)) RETURNS CHAR(50) BEGIN DECLARE v_hour INT; DECLAR' at line 1

我之前遇到过 1064 个错误,并且大部分都能够通过使用反引号 ` 或正常 tic ' 来解决它们。不幸的是,这一次没有帮助。

此函数的目标是计算两个日期之间的差异,并以小时和分钟的形式返回该差异。还要区分单数和复数。

如果有人有解决方案或线索告诉我如何解决这个问题,我将不胜感激。

谢谢您的帮助。现在解决了(我还必须将 DATE 更改为 DATETIME :))

4

1 回答 1

2

你有无效的声明。它应该是

CREATE FUNCTION flighttime (p_arrival DATE, p_departure DATE)
....

那么由于值的设置无效,另一个错误很快就会出现,你应该使用SET

SET v_minute = DATEDIFF(minute, p_departure, p_arrival);
SET v_hour = v_minute/60;
SET v_minute = v_minute-(v_hour*60);

完整的代码,

DELIMITER $$
CREATE FUNCTION flighttime (p_arrival DATE, p_departure DATE)
RETURNS CHAR(50)
BEGIN
    DECLARE v_hour INT;
    DECLARE v_minute INT;
    DECLARE v_htext CHAR(10);
    DECLARE v_mtext CHAR(10);
    DECLARE v_flighttime CHAR(50);
    SET v_minute = DATEDIFF(minute, p_departure, p_arrival);
    SET v_hour = v_minute/60;
    SET v_minute = v_minute-(v_hour*60);

    IF v_hour<2 AND v_minute<2 THEN 
        SET v_htext = ' hour and ';
        SET v_mtext=' minute';
    ELSEIF v_hour<2 AND v_minute>1 THEN 
        SET v_htext=' hour and '; 
        SET v_mtext=' minutes';
    ELSEIF v_hour>1 AND v_minute<2 THEN 
        SET v_htext=' hours and ';
        SET v_mtext=' minute';
    ELSE 
        SET v_htext=' hours and ';
        SET v_mtext=' minutes';
    END IF;

    SET v_flighttime = CONCAT(v_hour, v_mtext, v_minute, v_mtext);

    RETURN v_flighttime;
END $$
DELIMITER ;
于 2013-01-11T16:07:34.330 回答