snippetsqlMinor
How to select all columns plus one alias column in MySQL
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
I need something like this:
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.
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_NAMEAlso 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:
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
SELECT bin_to_uuid(id) as uuid, table_name.* FROM table_nameBut 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_nameCode Snippets
SELECT bin_to_uuid(id) as uuid, table_name.* FROM table_nameSELECT *, bin_to_uuid(id) as uuid FROM table_nameContext
StackExchange Database Administrators Q#257618, answer score: 6
Revisions (0)
No revisions yet.