MySQL ORDER BY无法使用双字别名

时间:2016-01-11 16:44:26

标签: mysql sql-order-by 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

此查询无效。但是当我尝试使用ORDER BY后跟1个单词时,它会起作用。知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

MySQL引擎不知道Last Name,但它知道lastname,因此您的查询应该是:

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

或者如果你想使用'Last Name',你应该在2 '中使用它,因为它是由空格分隔的2个单词。