0

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!

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843

2 Answers2

0

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
  • 253
  • 3
  • 15
-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
  • 6