Connecting mySQL table to itself to cross check results?












0















Not sure that's the best question title, but what I have is this:



db: usernames



id, username, search_via, value_1
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
4, Jim, Bill, 4
5, Brian, Bob, 5


I want to be able to search for the all the users with a search_via of 'Sue', so obviously I have:



SELECT * FROM usernames WHERE search_via = 'Sue':
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3


...but then I want, from those results, to also return any usernames which appear as a search_via as well, so in the case it would be 'Brian', as it is a search_via value for Bob, who appeared as a username for 'Sue':



5, Brian, Bob, 5


..so my full results should be:



1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
5, Brian, Bob, 5


...because my first search_via name was Sue, and in Sue's results, there was Bob, who was a search_via value for 'Brian'



...I've really struggled explaining this, so I have absolutely zero idea how to go about it?!?



Any ideas?










share|improve this question


















  • 1





    Do you want this search result to extend to third level and beyond as well ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:32











  • Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

    – Madhur Bhaiya
    Nov 14 '18 at 13:43











  • Yes, very much so.

    – TheBounder
    Nov 14 '18 at 13:44











  • "Yes, very much so" - Is this answer to my first question ? or the second question ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:44











  • You're right, 'Bill' should also appear. My mistake.

    – TheBounder
    Nov 14 '18 at 13:44
















0















Not sure that's the best question title, but what I have is this:



db: usernames



id, username, search_via, value_1
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
4, Jim, Bill, 4
5, Brian, Bob, 5


I want to be able to search for the all the users with a search_via of 'Sue', so obviously I have:



SELECT * FROM usernames WHERE search_via = 'Sue':
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3


...but then I want, from those results, to also return any usernames which appear as a search_via as well, so in the case it would be 'Brian', as it is a search_via value for Bob, who appeared as a username for 'Sue':



5, Brian, Bob, 5


..so my full results should be:



1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
5, Brian, Bob, 5


...because my first search_via name was Sue, and in Sue's results, there was Bob, who was a search_via value for 'Brian'



...I've really struggled explaining this, so I have absolutely zero idea how to go about it?!?



Any ideas?










share|improve this question


















  • 1





    Do you want this search result to extend to third level and beyond as well ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:32











  • Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

    – Madhur Bhaiya
    Nov 14 '18 at 13:43











  • Yes, very much so.

    – TheBounder
    Nov 14 '18 at 13:44











  • "Yes, very much so" - Is this answer to my first question ? or the second question ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:44











  • You're right, 'Bill' should also appear. My mistake.

    – TheBounder
    Nov 14 '18 at 13:44














0












0








0








Not sure that's the best question title, but what I have is this:



db: usernames



id, username, search_via, value_1
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
4, Jim, Bill, 4
5, Brian, Bob, 5


I want to be able to search for the all the users with a search_via of 'Sue', so obviously I have:



SELECT * FROM usernames WHERE search_via = 'Sue':
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3


...but then I want, from those results, to also return any usernames which appear as a search_via as well, so in the case it would be 'Brian', as it is a search_via value for Bob, who appeared as a username for 'Sue':



5, Brian, Bob, 5


..so my full results should be:



1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
5, Brian, Bob, 5


...because my first search_via name was Sue, and in Sue's results, there was Bob, who was a search_via value for 'Brian'



...I've really struggled explaining this, so I have absolutely zero idea how to go about it?!?



Any ideas?










share|improve this question














Not sure that's the best question title, but what I have is this:



db: usernames



id, username, search_via, value_1
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
4, Jim, Bill, 4
5, Brian, Bob, 5


I want to be able to search for the all the users with a search_via of 'Sue', so obviously I have:



SELECT * FROM usernames WHERE search_via = 'Sue':
1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3


...but then I want, from those results, to also return any usernames which appear as a search_via as well, so in the case it would be 'Brian', as it is a search_via value for Bob, who appeared as a username for 'Sue':



5, Brian, Bob, 5


..so my full results should be:



1, Rita, Sue, 1
2, Bob, Sue, 2
3, Bill, Sue, 3
5, Brian, Bob, 5


...because my first search_via name was Sue, and in Sue's results, there was Bob, who was a search_via value for 'Brian'



...I've really struggled explaining this, so I have absolutely zero idea how to go about it?!?



Any ideas?







mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 13:30









TheBounderTheBounder

244




244








  • 1





    Do you want this search result to extend to third level and beyond as well ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:32











  • Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

    – Madhur Bhaiya
    Nov 14 '18 at 13:43











  • Yes, very much so.

    – TheBounder
    Nov 14 '18 at 13:44











  • "Yes, very much so" - Is this answer to my first question ? or the second question ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:44











  • You're right, 'Bill' should also appear. My mistake.

    – TheBounder
    Nov 14 '18 at 13:44














  • 1





    Do you want this search result to extend to third level and beyond as well ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:32











  • Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

    – Madhur Bhaiya
    Nov 14 '18 at 13:43











  • Yes, very much so.

    – TheBounder
    Nov 14 '18 at 13:44











  • "Yes, very much so" - Is this answer to my first question ? or the second question ?

    – Madhur Bhaiya
    Nov 14 '18 at 13:44











  • You're right, 'Bill' should also appear. My mistake.

    – TheBounder
    Nov 14 '18 at 13:44








1




1





Do you want this search result to extend to third level and beyond as well ?

– Madhur Bhaiya
Nov 14 '18 at 13:32





Do you want this search result to extend to third level and beyond as well ?

– Madhur Bhaiya
Nov 14 '18 at 13:32













Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

– Madhur Bhaiya
Nov 14 '18 at 13:43





Why does n't 4, Jim, Bill, 4 also appear in output ? Bill is also searchable via Sue

– Madhur Bhaiya
Nov 14 '18 at 13:43













Yes, very much so.

– TheBounder
Nov 14 '18 at 13:44





Yes, very much so.

– TheBounder
Nov 14 '18 at 13:44













"Yes, very much so" - Is this answer to my first question ? or the second question ?

– Madhur Bhaiya
Nov 14 '18 at 13:44





"Yes, very much so" - Is this answer to my first question ? or the second question ?

– Madhur Bhaiya
Nov 14 '18 at 13:44













You're right, 'Bill' should also appear. My mistake.

– TheBounder
Nov 14 '18 at 13:44





You're right, 'Bill' should also appear. My mistake.

– TheBounder
Nov 14 '18 at 13:44












1 Answer
1






active

oldest

votes


















1














To get results upto second level hierarchy only, we can simply use a subquery to fetch all the username values where search_via = 'Sue'. We can then use WHERE search_via IN (..) to get second level hierarchy results as well.



SELECT * 
FROM usernames
WHERE search_via = 'Sue' OR
search_via IN (
SELECT username
FROM usernames
WHERE search_via = 'Sue'
)





share|improve this answer
























  • You are picking up an extra record (see here). Not that my answer seems correct either :-(

    – Tim Biegeleisen
    Nov 14 '18 at 13:40











  • @TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

    – Madhur Bhaiya
    Nov 14 '18 at 13:42











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%2f53301411%2fconnecting-mysql-table-to-itself-to-cross-check-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














To get results upto second level hierarchy only, we can simply use a subquery to fetch all the username values where search_via = 'Sue'. We can then use WHERE search_via IN (..) to get second level hierarchy results as well.



SELECT * 
FROM usernames
WHERE search_via = 'Sue' OR
search_via IN (
SELECT username
FROM usernames
WHERE search_via = 'Sue'
)





share|improve this answer
























  • You are picking up an extra record (see here). Not that my answer seems correct either :-(

    – Tim Biegeleisen
    Nov 14 '18 at 13:40











  • @TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

    – Madhur Bhaiya
    Nov 14 '18 at 13:42
















1














To get results upto second level hierarchy only, we can simply use a subquery to fetch all the username values where search_via = 'Sue'. We can then use WHERE search_via IN (..) to get second level hierarchy results as well.



SELECT * 
FROM usernames
WHERE search_via = 'Sue' OR
search_via IN (
SELECT username
FROM usernames
WHERE search_via = 'Sue'
)





share|improve this answer
























  • You are picking up an extra record (see here). Not that my answer seems correct either :-(

    – Tim Biegeleisen
    Nov 14 '18 at 13:40











  • @TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

    – Madhur Bhaiya
    Nov 14 '18 at 13:42














1












1








1







To get results upto second level hierarchy only, we can simply use a subquery to fetch all the username values where search_via = 'Sue'. We can then use WHERE search_via IN (..) to get second level hierarchy results as well.



SELECT * 
FROM usernames
WHERE search_via = 'Sue' OR
search_via IN (
SELECT username
FROM usernames
WHERE search_via = 'Sue'
)





share|improve this answer













To get results upto second level hierarchy only, we can simply use a subquery to fetch all the username values where search_via = 'Sue'. We can then use WHERE search_via IN (..) to get second level hierarchy results as well.



SELECT * 
FROM usernames
WHERE search_via = 'Sue' OR
search_via IN (
SELECT username
FROM usernames
WHERE search_via = 'Sue'
)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:32









Madhur BhaiyaMadhur Bhaiya

19.5k62236




19.5k62236













  • You are picking up an extra record (see here). Not that my answer seems correct either :-(

    – Tim Biegeleisen
    Nov 14 '18 at 13:40











  • @TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

    – Madhur Bhaiya
    Nov 14 '18 at 13:42



















  • You are picking up an extra record (see here). Not that my answer seems correct either :-(

    – Tim Biegeleisen
    Nov 14 '18 at 13:40











  • @TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

    – Madhur Bhaiya
    Nov 14 '18 at 13:42

















You are picking up an extra record (see here). Not that my answer seems correct either :-(

– Tim Biegeleisen
Nov 14 '18 at 13:40





You are picking up an extra record (see here). Not that my answer seems correct either :-(

– Tim Biegeleisen
Nov 14 '18 at 13:40













@TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

– Madhur Bhaiya
Nov 14 '18 at 13:42





@TimBiegeleisen thanks for the fiddle. Actually OP's expected output is wrong. His sample data covers all the usernames actually. Remaining two users are at second level.

– Madhur Bhaiya
Nov 14 '18 at 13:42




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301411%2fconnecting-mysql-table-to-itself-to-cross-check-results%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python