In the process of refactoring some MySQL queries in old PHP code, I came across some interesting syntax for field aliases in the existing code and poked around a bit to see what syntax variations exist.
// basic SELECT syntax SELECT `field_name` AS mycolumnname FROM `table`; // alias with a space(s) in the name SELECT `field_name` AS 'My Column Name' FROM `table`; // AS is optional SELECT `field_name` 'My Column Name' FROM `table`;
In MySQL, you can use the alias in your queries as follows:
SELECT `field_name` AS mycolumnname FROM `table` ORDER BY mycolumnname ASC;