当 Trip_days 用于计算 DailyCostAverage 并且它返回正确的数字时,为什么它的值是 NULL

问题描述 投票:0回答:1

我有这个程序,根据旅行的天数计算旅行的总成本和每日平均成本。我正在使用 DATEDIFF 来计算 trip_days。 我只是很困惑为什么它显示为空,但我用它来计算每日平均成本,它实际上给出了正确的答案。谢谢!

看起来像这样:

trip_days 为空,但 daily_cost_average 不为空。为什么?

这是程序:

DELIMITER //

CREATE PROCEDURE usp_update_activity_cost_get_total_trip_cost( IN activity_id_in INT,IN new_cost_in DECIMAL(10,2),OUT trip_name VARCHAR(255),OUT activity_name VARCHAR(255),OUT current_total_cost DECIMAL(10,2), OUT daily_cost_average DECIMAL(10,2), OUT trip_days INT)

BEGIN DECLARE this_trip_id INT;DECLARE trip_start_date DATE;DECLARE trip_end_date DATE;DECLARE trip_days INT ;


-- retrieve the associated trip_id, trip name, and activity name for the input activity_id
SELECT a.trip_id, t.name, a.name 
INTO this_trip_id, trip_name, activity_name
FROM activities a
JOIN trips t ON a.trip_id = t.trip_id
WHERE a.activity_id = activity_id_in
LIMIT 1;


-- if the activity id exists, update the cost of activity in the activities table
IF activity_id_in IS NOT NULL THEN
    -- update the cost of the activity
    UPDATE activities
    SET cost_in_cad = new_cost_in
    WHERE activity_id = activity_id_in;

    -- calculate the updated total cost for the trip the activity id is associated with
    SELECT SUM(cost_in_cad) 
    INTO current_total_cost
    FROM activities
    WHERE trip_id = this_trip_id 
    LIMIT 1;

    -- retrieve the trip's start and end dates to use it for calculating the number of days of the trip
    SELECT start_date, end_date 
    INTO trip_start_date, trip_end_date
    FROM trips
    WHERE trip_id = this_trip_id
    LIMIT 1;

    -- calculate the total number of days of the trip
    -- adding 1 so that if both start and end dates are on the same day, the days won't be zero
    
    SET trip_days = DATEDIFF(trip_end_date, trip_start_date) + 1;

    -- calculate the daily cost average using current total cost and number of days of the trip
    SET daily_cost_average = current_total_cost / trip_days;
 
ELSE
    -- if the activity id does not exist, set all outputs to NULL
    SET trip_name = NULL;
    SET activity_name = NULL;
    SET current_total_cost = NULL;
    SET daily_cost_average = NULL;
    SET trip_days = NULL;
END IF;

END //

DELIMITER ; 

我多次尝试更改 trip_days 的数据类型,但它仍然返回 NULL。 我尝试了不同的值,但它仍然是相同的。 我也在我朋友的电脑上尝试过,他也得到了NULL

这就是我的使用方式:

SET @activity_id_in = 1;
SET @new_cost_in = 250.00;
SET @trip_name = NULL;
SET @activity_name = NULL;
SET @current_total_cost = NULL;
SET @daily_cost_average = NULL;
SET @trip_days = NULL;

CALL usp_update_activity_cost_get_total_trip_cost(
    @activity_id_in, @new_cost_in, 
    @trip_name, @activity_name, @current_total_cost, @daily_cost_average, @trip_days
);

SELECT 
    @trip_name AS TripName,
    @activity_name AS ActivityName,
    @current_total_cost AS TotalTripCost,
    @daily_cost_average AS DailyCostAverage,
    @trip_days AS TripDays;

非常感谢!!

mysql database
1个回答
0
投票
CREATE PROCEDURE usp_update_activity_cost_get_total_trip_cost(
  ...
  OUT trip_days INT
)
BEGIN
  ...
  DECLARE trip_days INT;

您有一个名为

trip_days
的输出变量和一个也名为
trip_days
的局部变量。

  SET trip_days = DATEDIFF(trip_end_date, trip_start_date) + 1;

这设置该名称的局部变量,而不是 OUT 变量。

您实际上根本不需要局部变量

trip_days

只需删除声明:

  DECLARE trip_days INT; -- remove this line

如果您认为需要局部变量,请重命名局部变量或 OUT 变量,以使它们不相同。然后将 DATEDIFF 的值分配给 OUT 变量,并在后续的 daily_cost_average 计算中使用该值。

然后该过程开始工作并为 trip_days 输出一个非 NULL 值。

© www.soinside.com 2019 - 2024. All rights reserved.