Add a new column to SELECT if elements of an INNER JOIN exists












0














I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).



This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).



select co.name from COMPANIES co  
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;


I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.



Something like "isCustomer"



select co.name, isCustomer from COMPANIES co ...









share|improve this question






















  • I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
    – Gordon Linoff
    Nov 19 '18 at 18:59












  • @GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:05
















0














I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).



This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).



select co.name from COMPANIES co  
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;


I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.



Something like "isCustomer"



select co.name, isCustomer from COMPANIES co ...









share|improve this question






















  • I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
    – Gordon Linoff
    Nov 19 '18 at 18:59












  • @GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:05














0












0








0







I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).



This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).



select co.name from COMPANIES co  
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;


I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.



Something like "isCustomer"



select co.name, isCustomer from COMPANIES co ...









share|improve this question













I have 3 table PERSONS, COMPANIES and PERSON_CUSTOMER_COMPANY which makes the relation n-to-n if the person is a customer of the company (a person may be others relations with each company).



This query returns all companies that have a relation with a given person as a Customer (line 3 inner join).



select co.name from COMPANIES co  
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
INNER JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;


I need to change this query to return all companies from a person even if they are not related in the PERSON_CUSTOMER_COMPANY and an extra field indicating if the person is a customer of the company.



Something like "isCustomer"



select co.name, isCustomer from COMPANIES co ...






sql hibernate






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 18:53









Thadeu Antonio Ferreira MeloThadeu Antonio Ferreira Melo

194113




194113












  • I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
    – Gordon Linoff
    Nov 19 '18 at 18:59












  • @GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:05


















  • I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
    – Gordon Linoff
    Nov 19 '18 at 18:59












  • @GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:05
















I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59






I don't get your data model. All three tables don't seem necessary for this. How is person connected to company?
– Gordon Linoff
Nov 19 '18 at 18:59














@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05




@GordonLinoff I don´t understand it in full either. It is a legacy system and I had to obscure some details.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:05












2 Answers
2






active

oldest

votes


















1














An inner join will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company table, then you need an outer join instead. Then you can use a case statement to create the new column:



SELECT co.name, 
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;





share|improve this answer





















  • Thank you man. That is exactly what I need.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:02



















0














I would probably use exists:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


This only uses PERSON, because that is the JOIN you use in your query.



I suspect you really want:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


In either case, only two table are necessary.






share|improve this answer





















  • This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
    – sgeddes
    Nov 19 '18 at 19:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380959%2fadd-a-new-column-to-select-if-elements-of-an-inner-join-exists%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














An inner join will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company table, then you need an outer join instead. Then you can use a case statement to create the new column:



SELECT co.name, 
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;





share|improve this answer





















  • Thank you man. That is exactly what I need.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:02
















1














An inner join will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company table, then you need an outer join instead. Then you can use a case statement to create the new column:



SELECT co.name, 
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;





share|improve this answer





















  • Thank you man. That is exactly what I need.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:02














1












1








1






An inner join will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company table, then you need an outer join instead. Then you can use a case statement to create the new column:



SELECT co.name, 
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;





share|improve this answer












An inner join will only return results that match in both tables. Since you are looking for potential companies that don't have records in the person_customer_company table, then you need an outer join instead. Then you can use a case statement to create the new column:



SELECT co.name, 
CASE WHEN pcc.Person_id IS NULL then 'No' else 'Yes' End as IsCustomer
FROM COMPANIES co
INNER JOIN PERSONS p on p.COMPANY_ID = co.id
LEFT JOIN PERSON_CUSTOMER_COMPANY pcc on pcc.PERSON_ID = p.PERSON_ID
WHERE p.PERSON_ID = 123456;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:57









sgeddessgeddes

56.5k53963




56.5k53963












  • Thank you man. That is exactly what I need.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:02


















  • Thank you man. That is exactly what I need.
    – Thadeu Antonio Ferreira Melo
    Nov 19 '18 at 19:02
















Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02




Thank you man. That is exactly what I need.
– Thadeu Antonio Ferreira Melo
Nov 19 '18 at 19:02













0














I would probably use exists:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


This only uses PERSON, because that is the JOIN you use in your query.



I suspect you really want:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


In either case, only two table are necessary.






share|improve this answer





















  • This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
    – sgeddes
    Nov 19 '18 at 19:03
















0














I would probably use exists:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


This only uses PERSON, because that is the JOIN you use in your query.



I suspect you really want:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


In either case, only two table are necessary.






share|improve this answer





















  • This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
    – sgeddes
    Nov 19 '18 at 19:03














0












0








0






I would probably use exists:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


This only uses PERSON, because that is the JOIN you use in your query.



I suspect you really want:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


In either case, only two table are necessary.






share|improve this answer












I would probably use exists:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON p
WHERE p.COMPANY_ID = co.id AND
p.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


This only uses PERSON, because that is the JOIN you use in your query.



I suspect you really want:



SELECT co.name, 
(CASE WHEN EXISTS (SELECT 1
FROM PERSON_CUSTOMER_COMPANY pcc
WHERE pcc.COMPANY_ID = co.id AND
pcc.PERSON_ID = 123456
)
THEN 'Yes' ELSE 'No'
END) as IsCustomer
FROM COMPANIES co;


In either case, only two table are necessary.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 19:00









Gordon LinoffGordon Linoff

761k35294399




761k35294399












  • This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
    – sgeddes
    Nov 19 '18 at 19:03


















  • This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
    – sgeddes
    Nov 19 '18 at 19:03
















This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
– sgeddes
Nov 19 '18 at 19:03




This returns all companies, not just those where the person has a relationship... OP states return all companies from a person - I think this is why the join is needed.
– sgeddes
Nov 19 '18 at 19:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380959%2fadd-a-new-column-to-select-if-elements-of-an-inner-join-exists%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory