简体   繁体   English

为数据库 MySQL 创建 function

[英]Creation function for database MySQL

I have a database 'excursions' with information about customers, buses, drivers, bills and excursions.我有一个数据库“远足”,其中包含有关客户、公共汽车、司机、账单和远足的信息。 I need to create a function that calculates payment for a specified month for one customer.我需要创建一个 function 来计算一个客户指定月份的付款。 I ve already created it. Also ive already created it. Also i ve already created it. Also i ve created a procedure that call that function for all customers. ve already created it. Also i为所有客户创建了一个调用 function 的程序。 But MySQL Workbench tell me that there is a mistake.但是 MySQL Workbench 告诉我有错误。 Help me to fix it帮我修一下

USE excursions_test;
DELIMITER $$
CREATE FUNCTION  sum_bill(excursion_id INT, for_begin DATE, for_end DATE) RETURNS INT
DETERMINISTIC
    BEGIN
       SELECT excStart = excursion.start_date, excEnd = excursion.end_date, excPrice =( excursion.excursion_duration*excursion.number_of_tourists*bus_model.fuel_for_km*excursion.distance)*1.5,
        IF(excStart < for_begin OR excEnd > for_end, 0, 
        IF(excStart <= for_begin OR excEnd >= for_end AND excEnd <= for_end, DATEDIFF(for_begin, excEnd)*excPrice/30,
        IF(excStart >= for_begin AND excEnd >= for_begin AND excEnd <= for_end, DATEDIFF(excStart, excEnd)*excPrice/30,
        IF(excStart >= for_begin AND excEnd >= for_end, DATEDIFF(excStart, for_end)*excPrice/30,
        IF(excStart <= for_begin AND excEnd >= for_end, DATEDIFF(for_begin, for_end)*excPrice/30, 0)))))
        FROM excursion JOIN bus_model ON excursion.bus_model_id = bus_model.bus_model_id
        WHERE excursion.excursion_id = excursion_id;
        RETURN 0;
    END $$
    DELIMITER ;

DELIMITER $$
CREATE PROCEDURE bill_creator(for_begin DATE, for_end DATE)
BEGIN
    INSERT INTO bill(excursion_id, start_date, end_date, amount)
    SELECT excursion.excursion_id, for_begin, for_end, dbo.sum_bill(dbo.excursion.excursion_id, for_begin, for_end)
    FROM excursion
    WHERE (excursion.start_date >= for_begin AND excursion.end_date <= for_end)
        OR (excursion.end_date >= for_begin AND excursion.end_date <= for_end)
        OR (excursion.start_date<= for_begin AND excursion.end_date>= for_begin);
END $$
DELIMITER ;

CALL bill_creator ('2021-10-01', '2021-10-31')

The mistake: Not allowed to return a result set from a function错误:不允许从 function 返回结果集

You are returning a single row resultset that has multiple columns but the return type of the function is int.您正在返回具有多列的单行结果集,但 function 的返回类型是 int。

I haven't tested the following so you might need to tweak it, but the error should be fixed if you replace the section我尚未测试以下内容,因此您可能需要对其进行调整,但是如果您替换该部分,则应修复该错误

   SELECT excStart = excursion.start_date, excEnd = excursion.end_date, excPrice =( excursion.excursion_duration*excursion.number_of_tourists*bus_model.fuel_for_km*excursion.distance)*1.5,
    IF(excStart < for_begin OR excEnd > for_end, 0, 
    IF(excStart <= for_begin OR excEnd >= for_end AND excEnd <= for_end, DATEDIFF(for_begin, excEnd)*excPrice/30,
    IF(excStart >= for_begin AND excEnd >= for_begin AND excEnd <= for_end, DATEDIFF(excStart, excEnd)*excPrice/30,
    IF(excStart >= for_begin AND excEnd >= for_end, DATEDIFF(excStart, for_end)*excPrice/30,
    IF(excStart <= for_begin AND excEnd >= for_end, DATEDIFF(for_begin, for_end)*excPrice/30, 0)))))
    FROM excursion JOIN bus_model ON excursion.bus_model_id = bus_model.bus_model_id
    WHERE excursion.excursion_id = excursion_id;
    RETURN 0;

with

SELECT excStart = excursion.start_date, excEnd = excursion.end_date, excPrice =( excursion.excursion_duration*excursion.number_of_tourists*bus_model.fuel_for_km*excursion.distance)*1.5;
    FROM excursion JOIN bus_model ON excursion.bus_model_id = bus_model.bus_model_id
    WHERE excursion.excursion_id = excursion_id;

IF(excStart < for_begin OR excEnd > for_end)
BEGIN
    RETURN 0;
END;

IF(excStart <= for_begin OR excEnd >= for_end AND excEnd <= for_end)
BEGIN
    RETURN  DATEDIFF(for_begin, excEnd)*excPrice/30;
END;

IF(excStart >= for_begin AND excEnd >= for_begin AND excEnd <= for_end)
BEGIN
    RETURN  DATEDIFF(excStart, excEnd)*excPrice/30;
END;

IF(excStart >= for_begin AND excEnd >= for_end)
BEGIN
    RETURN  DATEDIFF(excStart, for_end)*excPrice/30;
END;

IF(excStart <= for_begin AND excEnd >= for_end)
BEGIN
    RETURN  DATEDIFF(for_begin, for_end)*excPrice/30, 0;
END;

RETURN 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM