MySql - use of ALIAS for multiple columns

北城以北 提交于 2019-12-08 07:22:47

问题


I saw the following code:

SELECT 
    u.ID, u.username, u.active, u.email, u.admin, u.banned, 
    u.name AS groupmemberships 
FROM users u 
WHERE u.ID={$uid}

and was wondering where the official documentation about aliasing multiple columns was. W3schools (not the best source) as the only place where I found "documentation" in the following way:

SELECT column_name(s) FROM table_name AS alias_name;

http://www.w3schools.com/sql/sql_alias.asp

I would appreciate a link to official documentation so I can look it over.


回答1:


You can't use the same alias for multiple columns, but you can concatenate values and give the result an alias:

SELECT 
  u.ID, u.username, u.active, u.email, u.admin, u.banned, 
  u.name + u.username AS groupmemberships 
FROM users u 

If this is what you want, then check here for how to deal with null values.




回答2:


You can use concat function to get single alias for multiple columns,

 select concat(first_name, ' ', last_name) as employee_name from user;


来源:https://stackoverflow.com/questions/18092811/mysql-use-of-alias-for-multiple-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!