[英]Creation function for database MySQL
我有一个数据库“远足”,其中包含有关客户、公共汽车、司机、账单和远足的信息。 我需要创建一个 function 来计算一个客户指定月份的付款。 我ve already created it. Also i
ve already created it. Also i
为所有客户创建了一个调用 function 的程序。 但是 MySQL Workbench 告诉我有错误。 帮我修一下
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')
错误:不允许从 function 返回结果集
您正在返回具有多列的单行结果集,但 function 的返回类型是 int。
我尚未测试以下内容,因此您可能需要对其进行调整,但是如果您替换该部分,则应修复该错误
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;
和
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.