sql - Join two tables, not joined with any foreign keys and different unrelated ids -


i have 2 tables.

companynames: company_name text (unique) companyname_id bigint   homenicknames: home_nickname text (unique) nickname_id bigint 

so, rows like:

    companynames     ==============     company_name   ||   companyname_id |:--------------------------------------:|      robert         |      123      mr.robert      |      123      mr.robertgrant |      123        homenicenames     ==============     home_nickname  ||   nickname_id |:-------------------------------------:|      robert         |      431      robb           |      431      dearrobb       |      431 

assume every company name , nickname can joined because of 1 same company name , nickname.

i need query can generate following result:

       name_id || home_nickname |:-----------------------------------:|        123     |   robb        123     |   robert        123     |   dearrobb  

find robertfirst , nicks id:

select     c.companyname_id   ,n2.home_nickname companynames c join homenicenames n   on c.company_name = n.home_nickname -- match robert join homenicenames n2   on n.nickname_id = n2.nickname_id   -- match nickname same id robert 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -