ms access - SQL query. I would like to create a search that returns either the name of a contact, or the name of a company(and it's contact) -


i have 3 tables (because involves many many relationship):

tblcontact     tblcontcompjunction   tblcompany contactid      contactid             companyid           contactname    companyid             companyname 

currently query built such:

select contactid, companyid, contactname, companyname,(contactname & companyname) searchconcat (tblcontact left join tblcontcompjunction on tblcontact.contactid = tblcontcompjunction.contactid)  left join tblcompany on tblcontcompjunction.companyid = tblcompany.companyid; 

the problem have can't contact listed alone when has relatable company. need search determine between selecting company , contact, , fails.

based on comment, want each contactname "once each company , once himself."

this query give each contactname himself.

select     tblcontact.contactname,     'himself' companyname tblcontact 

and query give row each contactname/companyname pair.

select     tblcontact.contactname,     tblcompany.companyname     (tblcontact     inner join tblcontcompjunction     on tblcontact.contactid = tblcontcompjunction.contactid)     inner join tblcompany     on tblcontcompjunction.companyid = tblcompany.companyid 

use access' query designer set joins in case made error in untested sql.

once have both queries working correctly, can use union query combine them.

select     tblcontact.contactname,     'himself' companyname tblcontact union select     tblcontact.contactname,     tblcompany.companyname     (tblcontact     inner join tblcontcompjunction     on tblcontact.contactid = tblcontcompjunction.contactid)     inner join tblcompany     on tblcontcompjunction.companyid = tblcompany.companyid 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -