1

Hi Guys I'm doing inner join for two tables and selecting all the columns from both tables. But I'm getting three cols with same name like id, created_at and updated_at.

Query:

SELECT addresses.* , facilities.* FROM facilities
      INNER JOIN addresses
      ON facilities.main_address_id = addresses.id

Is there any possible way that I can mention alias for above cols having same name while selecting all cols with * ?

Help of any kind would be appreciated! Thanks!

Nikhil Attar
  • 105
  • 11
  • 1
    No, there is no automatic way to rename columns. Related (if not duplicate): https://stackoverflow.com/questions/5179648 –  Feb 10 '21 at 09:25

2 Answers2

1

No you can't do this other than aliasing each column separately.

But if your query will be repetitive you could create VIEW:

CREATE OR REPLACE VIEW facilities_addresses AS
SELECT 
    addresses.column AS "addresses_column", 
    facilities.column AS "facilities_column"
FROM facilities
    INNER JOIN addresses ON (facilities.main_address_id = addresses.id)

and then you can query:

SELECT * FROM facilities_addresses
LordF
  • 407
  • 5
  • 18
-1

yes you can

SELECT 
   addr.id as addressesId ,
   addr.created_at as addresses_created_at,
   addr.updated_at as addresses_update_at,
   fac.id as facilitiesId,
   fac.created_at as facilities_created_at,
   fac.updated_at as facilities_updated_at FROM facilities as fac
      INNER JOIN addresses as addr
      ON facilities.main_address_id = addresses.id
dgnk
  • 98
  • 8