HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to select all columns plus one alias column in MySQL

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnsallcolumnplusonemysqlhowselectalias

Problem

It is a simple question, but i could not find the answer.

I want to select all the columns in my table with a SELECT * statement, but i also need to alias a UUID column this way: BIN_TO_UUID(ID) as ID.

I need something like this:

SELECT BIN_TO_UUID(ID) AS ID, * FROM TABLE_NAME


Also would be nice to remove the original column that was aliased, to do not have the same column twice with different names, but just if this do not add more text to the query, because i will have to repeat this for all my 16 tables.

Solution

I figured out the solution. I simple had do repeat the table name, as follows:

SELECT bin_to_uuid(id) as uuid, table_name.* FROM table_name


But if you have a solution that does not require to repeat the table name i will upvote your answer, since repeating all the table names in all the queries will bloat my the code a little.

EDIT:

As suggested in the comments by @ypercubeᵀᴹ and @Akina, looks like the selector position is limited in MySQL. If you move the to the first position everything works fine:

SELECT *, bin_to_uuid(id) as uuid FROM table_name

Code Snippets

SELECT bin_to_uuid(id) as uuid, table_name.* FROM table_name
SELECT *, bin_to_uuid(id) as uuid FROM table_name

Context

StackExchange Database Administrators Q#257618, answer score: 6

Revisions (0)

No revisions yet.