sql - Left joining two reference tables to get the proper value -


i have table client data, , must join 2 reference tables in order desired text value. somehow did not include data in 1 reference table, of data need value table.

first table data

id         clientid            companyid 1          199                 80 2          187                 91 3          85                  1001 4          83                  1145 

company id can seen varies 80 - 1200

the problem is, values > 1000 stored in reference table companies, so

tblcompanies  companyid         companyname 1001              microsoft 1002              apple 1145              hp 

the rest of company names stored in table

tblreference  id        fldname       label 80        company       adobe 81        company       amazon 91        company       pixel 

tblreference has other data part reference id's 80 - 90, , other companyid's tblcompanies....

how able join 2 tables? without getting duplicate records. tried union , unioan didn't work out

first union tblcompanies , tblreferences tables together, join:

select t1.id, t1.clientid, t1.companyid, t2.companyname firsttable t1     inner join          (              select companyid, companyname tblcompanies              union              select id, label tblreference fldname = 'company'         ) t2 on t1.companyid = t2.companyid 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -