I am in the process to move a really small database from Access to MySQL and I have some queries to translate.
I have this really simple query (in Access db):
SELECT InvoiceNumber,
Field1 + Field2 AS CalcField1,
Field3 + Field4 AS CalcField2,
(CalcField1 - CalcField2) / CalcField2 AS Percentage ;
This does not work in MySQL, because it does not allow column aliases in formulas. I learnt here that the solution is to wrap the alias with a (SELECT alias)
. That works, but the result is really ugly and becomes unreadable. In my case it is:
((SELECT ((SELECT CalcField1) - (SELECT CalcField2))) / (SELECT CalcField2)) AS Percentage
In the former case I can understand immediately the expression, in the latter I cannot.
My question is: is there an alternative way to use aliases in MySQL formulas that have a better readability?