1

I need to select from a database some tables with many columns. So I want to select

select t1.*,t2.*,t3.*
from table1 t1 
left outer join table2 t2 on ...
left outer join tabl 3 t3 on ...
...

Some keys are identically named, but - due to the outer command - sometimes null. Unfortunately I cannot directly see whether the key comes from t1, t2 or t3. Is there any chance to add automatically the tablenames or another separating/distinguishing qualifier to all column names, i.e. t1_thekey, t2_thekey, t3_thekey ...?

3
  • 1
    Not automatically. You need to rename each column one at a time. Commented Sep 21, 2020 at 12:45
  • To add to the above, many consider SELECT * to be undesirable, because its meaning changes as the number of columns in the table may change. By explicitly listing out each column, you avoid this problem. Commented Sep 21, 2020 at 12:47
  • It becomes more tricky as I use it inside node-red. Since I get a JSON back, each key (= column name) is allowed only once. This results in overwriting existing values with null by the missing data set from one of the left outer tables. As there is no solution I definitely have to rename the columns manually. Commented Sep 21, 2020 at 13:08

1 Answer 1

2

Postgres doesn't allow you to rename the columns en masse. However, you could select the columns as records, so you can see where they come from:

select t1, t2, t3
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, interesting workaround. Thank you. ... But the column names are than missing?!
@MichaelHecht . . . You have a record name and then you have a field name within the records.

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.