SQL 中基于 datediff 和 curdate 函数计算列值

Calculating a column value based on datediff and curdate function in SQL

是否可以按如下方式计算以下字段:

如果 DateDiff 中有整数,则 LateFee 列将由 (DateDiff * 3) 填充 如果 DateDiff 中没有值,即租金仍未支付,则当前到期的滞纳金按 (Date_Due - 当前日期) * 3

计算

我是通过存储过程尝试的:

delimiter $$
create procedure calcLateFees()
begin 
select Rental_ID as "Rental ID", Platform_ID as "Platform ID",Title_ID as "Title ID",Date_Due as "Due Date",
Date_Returned as "Returned On", Datediff(CURDATE(), Date_Due) * 3 as "Late Fee Outstanding"
from tp_rental
where month (curdate() = month(now()) and CURDATE() > Date_Due;
end $$

call calcLateFees();

我可能做错了,所以非常感谢任何帮助。提前谢谢你。

if there is an integer in DayDiff then the LateFee column will be populated by (DayDiff * 3). If there is no value in DayDiff [...] then the LateFee that is due currently is calculated by (Date_Due - current date) * 3

我会将其翻译为以下 update 查询:

update tp_rental
set LateFee = case
    when DayDiff is not null then DayDiff * 3
    else datediff(date_due, curdate()) * 3
end