我上周刚开始使用MySQL,我正在尝试编写代码来检查特定地址的最早计费记录实例,并将其标记为“新”。它还应识别帐户是否已被取消,并将取消记录的第一个实例标记为“新”。代码编译,但每当我尝试通过输入地址来测试它时,我会收到“错误代码:1054。'where子句'中的未知列'BillingRecords.Address'”我觉得我已经尝试了列引用的每个修复在阳光下(有/没有反引号,单引号,数据库名称,表名等)。我应该如何引用代码中的列以避免此错误?此外,由于我无法正确测试,因此代码中可能还有一些其他错误。如果你发现任何事情,我真的很感激一些反馈。
SELECT Min(`Date`) INTO earliestDate FROM Billing.BillingRecords WHERE `Address` = addressToCheck; #Determines the earliest date of all instances of the specified address
SELECT Min(`Date`) INTO earliestCancelDate FROM Billing.BillingRecords WHERE `Address` = addressToCheck AND `Cancel` = "Cancelled"; #Determines the earliest date of all instances of the specified address where the account has been marked as "Cancelled"
CASE
WHEN `BillingRecords`.`Address` = addressToCheck AND `BillingRecords`.`Date` = earliestDate THEN
SET isItNew = "NEW"; #Returns "NEW" if the address matches the specified address and the date matches the earliest date
WHEN `BillingRecords`.`Address` = addressToCheck AND `BillingRecords`.`Date` != earliestDate THEN
SET isItNew = "OLD"; #Returns "OLD" if the address matches the specified address and the date does not match the earliest date
ELSE
SET isItNew = NULL;
END CASE;
CASE
WHEN `BillingRecords`.`Address` = addressToCheck AND `BillingRecords`.`Date` = earliestCancelDate AND `BillingRecords`.`Cancel` = "Cancelled" THEN
SET isItNewCancel = "NEW"; #Returns "NEW" if the address matches the specified address, the account is marked as "Cancelled", and the date matches the earliest cancel date
WHEN `BillingRecords`.`Address` = addressToCheck AND `BillingRecords`.`Date` != earliestCancelDate AND `BillingRecords`.`Cancel` = "Cancelled" THEN
SET isItNewCancel = "OLD"; #Returns "OLD" if the address matches the specified address, the account is marked as "Cancelled", and the date does not match the earliest date
ELSE
SET isItNewCancel = NULL;
END CASE;