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;

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.