I just started working with MySQL last week, and I'm attempting to write a code to check for the earliest instance of a billing record for a certain address and mark it as "NEW". It should also identify if an account has since been cancelled and mark the first instance of a cancelled record as "NEW". The code compiles, but whenever I try to test it by inputting an address, I receive "Error Code: 1054. Unknown column 'BillingRecords.Address' in 'where clause'" I feel like I've tried every fix for the column references under the sun (with/without backticks, single quotes, database names, table names, etc.) How exactly should I be referencing the columns in the code to avoid this error? Also, since I haven't been able to test it properly, there may be a few other errors in the code. If you notice anything, I'd really appreciate some feedback.
Code in question
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;