1

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.

4
  • 3
    Is it aliasing multiple columns? Looks like this will just alias u.name AS groupmemberships the other columns will still be whatever they're normally called. Commented Aug 7, 2013 at 0:44
  • I agree with @Taryn East, and we generally use alias when joining tables. Commented Aug 7, 2013 at 1:24
  • Robert, there are hundreds of on-line explanations of SQL, and dozens of them are official (because it is provided by many softwares; some differences, but not great). Suggest you run a search on keywords MySQL ALIAS. Commented Aug 7, 2013 at 14:48
  • You can alias an expression or column, such as in select u.id as user_id and select upper(u.username) as uppercase_name, and you can alias a table as in from users u. There exists no group alias for multiple columns. Does this answer your question? Well, you haven't really asked a question, though ... Commented Sep 6, 2018 at 11:32

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.