user123456789
user123456789

Reputation: 25

MySQL ORDER BY not working using 2-word alias

SELECT
  u.user_id AS 'ID'
, u.username AS 'Username' 
, u.lastname AS 'Last Name' 
, r.rolename AS 'Role' 
FROM user AS u , role AS r  
ORDER BY Last Name

This query is not working. But it works when I try to use the ORDER BY followed by 1 word. Any idea how to solve it?

Upvotes: 2

Views: 1378

Answers (1)

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

MySQL engine doesn't know Last Name, but it knows lastname, so your query should be:

SELECT u.user_id AS 'ID' , u.username AS 'Username' , u.lastname AS 'Last Name' ,
r.rolename AS 'Role' FROM user AS u , role AS r ORDER BY u.lastname

Or if you want to use 'Last Name' you should use it enclosed in 2 ', because it is 2 words separated by space.

Upvotes: 1

Related Questions