SQL select join: is it possible to prefix all columns as ‘prefix.*’?

I’m wondering if this is possible in SQL. Say you have two tables A and B, and you do a select on table A and join on table B:

SELECT a.*, b.* FROM TABLE_A a JOIN TABLE_B b USING (some_id);

If table A has columns ‘a_id’, ‘name’, and ‘some_id’, and table B has ‘b_id’, ‘name’, and ‘some_id’, the query will return columns ‘a_id’, ‘name’, ‘some_id’, ‘b_id’, ‘name’, ‘some_id’. Is there any way to prefix the column names of table B without listing every column individually? The equivalent of this:

SELECT a.*, b.b_id as 'b.b_id', b.name as 'b.name', b.some_id as 'b.some_id'
FROM TABLE_A a JOIN TABLE_B b USING (some_id);

But, as mentioned, without listing every column, so something like:

SELECT a.*, b.* as 'b.*'
FROM TABLE_A a JOIN TABLE_B b USING (some_id);

Basically something to say, “prefix every column returned by b.* with ‘something'”. Is this possible or am I out of luck?

EDITS

Advice on not using SELECT * and so on is valid advice but not relevant in my context, so please stick to the problem at hand — is it possible to add a prefix (a constant specified in the SQL query) to all the column names of a table in a join?

My ultimate goal is to be able to do a SELECT * on two tables with a join, and be able to tell, from the names of the columns I get in my result set, which columns came from table A and which columns came from table B. Again, I don’t want to have to list columns individually, I need to be able to do a SELECT *.

26 Answers
26

Leave a Comment