alias column names without mentioning column name

Viewed 38

I'm trying to get all columns from each table with a prefix in the output, without mentioning all column names specifically in the select statement. Like:

SELECT * 
FROM TABLE1 as T1
FULL JOIN TABLE2 as T2
ON T1.number=T2.number

Where I would want to get all column names from table1 and table2 prefixed with "T1" and "T2".

Many thanks in advance!

1 Answers
        SELECT
    CONCAT('T1', COLUMN_NAME), ORDINAL_POSITION
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_NAME = 'TABLE1'
ORDER BY 2                                                          
UNION                                                              
    SELECT CONCAT('T2', COLUMN_NAME), ORDINAL_POSITION
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    TABLE_NAME = 'TABLE2'
ORDER BY 2
Related