Selecting a list of data which can contain values from 2 columns











up vote
1
down vote

favorite












I have a friend Request table which holds a friend request data between two users (recipient and sender) and also the friendrequeststatus (ACCEPTED, REJECTED):



enter image description here



i want to retrieve the friends of the User with id of 1, which include 2, 3, 4 and if possible for them by order of name.



By friend i mean (where a user e.g 1, is either a sender or recipient to another user e.g 2 and the friendrequeststatus has the value of ACCEPETED). By friends i mean all columns where the user e.g 1 whether as a recipient or sender has an accepted friendrequeststatus.



I tried
`



 select request.*
from friendrequest request
where 1 in (request.recipient, request.sender)


enter image description here










share|improve this question
























  • Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
    – Gordon Linoff
    Nov 19 at 11:53










  • Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
    – 404
    Nov 19 at 11:53










  • @GordonLinoff what name and also what information do you require ?
    – Oto-obong Eshiett
    Nov 19 at 11:56












  • @eurotrash i have modified my query sir, and also the question, thank you for the correction.
    – Oto-obong Eshiett
    Nov 19 at 12:08










  • @Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
    – 404
    Nov 19 at 12:13















up vote
1
down vote

favorite












I have a friend Request table which holds a friend request data between two users (recipient and sender) and also the friendrequeststatus (ACCEPTED, REJECTED):



enter image description here



i want to retrieve the friends of the User with id of 1, which include 2, 3, 4 and if possible for them by order of name.



By friend i mean (where a user e.g 1, is either a sender or recipient to another user e.g 2 and the friendrequeststatus has the value of ACCEPETED). By friends i mean all columns where the user e.g 1 whether as a recipient or sender has an accepted friendrequeststatus.



I tried
`



 select request.*
from friendrequest request
where 1 in (request.recipient, request.sender)


enter image description here










share|improve this question
























  • Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
    – Gordon Linoff
    Nov 19 at 11:53










  • Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
    – 404
    Nov 19 at 11:53










  • @GordonLinoff what name and also what information do you require ?
    – Oto-obong Eshiett
    Nov 19 at 11:56












  • @eurotrash i have modified my query sir, and also the question, thank you for the correction.
    – Oto-obong Eshiett
    Nov 19 at 12:08










  • @Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
    – 404
    Nov 19 at 12:13













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a friend Request table which holds a friend request data between two users (recipient and sender) and also the friendrequeststatus (ACCEPTED, REJECTED):



enter image description here



i want to retrieve the friends of the User with id of 1, which include 2, 3, 4 and if possible for them by order of name.



By friend i mean (where a user e.g 1, is either a sender or recipient to another user e.g 2 and the friendrequeststatus has the value of ACCEPETED). By friends i mean all columns where the user e.g 1 whether as a recipient or sender has an accepted friendrequeststatus.



I tried
`



 select request.*
from friendrequest request
where 1 in (request.recipient, request.sender)


enter image description here










share|improve this question















I have a friend Request table which holds a friend request data between two users (recipient and sender) and also the friendrequeststatus (ACCEPTED, REJECTED):



enter image description here



i want to retrieve the friends of the User with id of 1, which include 2, 3, 4 and if possible for them by order of name.



By friend i mean (where a user e.g 1, is either a sender or recipient to another user e.g 2 and the friendrequeststatus has the value of ACCEPETED). By friends i mean all columns where the user e.g 1 whether as a recipient or sender has an accepted friendrequeststatus.



I tried
`



 select request.*
from friendrequest request
where 1 in (request.recipient, request.sender)


enter image description here







sql postgresql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:19

























asked Nov 19 at 11:50









Oto-obong Eshiett

94111




94111












  • Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
    – Gordon Linoff
    Nov 19 at 11:53










  • Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
    – 404
    Nov 19 at 11:53










  • @GordonLinoff what name and also what information do you require ?
    – Oto-obong Eshiett
    Nov 19 at 11:56












  • @eurotrash i have modified my query sir, and also the question, thank you for the correction.
    – Oto-obong Eshiett
    Nov 19 at 12:08










  • @Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
    – 404
    Nov 19 at 12:13


















  • Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
    – Gordon Linoff
    Nov 19 at 11:53










  • Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
    – 404
    Nov 19 at 11:53










  • @GordonLinoff what name and also what information do you require ?
    – Oto-obong Eshiett
    Nov 19 at 11:56












  • @eurotrash i have modified my query sir, and also the question, thank you for the correction.
    – Oto-obong Eshiett
    Nov 19 at 12:08










  • @Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
    – 404
    Nov 19 at 12:13
















Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
– Gordon Linoff
Nov 19 at 11:53




Your table doesn't have the name. So, your question doesn't have enough information to answer the question you have asked.
– Gordon Linoff
Nov 19 at 11:53












Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
– 404
Nov 19 at 11:53




Please define the exact criteria. "Friends" means something to you but not to us. What is the relevance of time, which you use in your query but don't describe?
– 404
Nov 19 at 11:53












@GordonLinoff what name and also what information do you require ?
– Oto-obong Eshiett
Nov 19 at 11:56






@GordonLinoff what name and also what information do you require ?
– Oto-obong Eshiett
Nov 19 at 11:56














@eurotrash i have modified my query sir, and also the question, thank you for the correction.
– Oto-obong Eshiett
Nov 19 at 12:08




@eurotrash i have modified my query sir, and also the question, thank you for the correction.
– Oto-obong Eshiett
Nov 19 at 12:08












@Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
– 404
Nov 19 at 12:13




@Oto-obongEshiett Your question is clearer now; only thing remaining is, can you describe or show what the expected result should look like, based on the sample data you've shown? E.g. a single row with an array of friend ids, or one row per friend?
– 404
Nov 19 at 12:13












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Setup:



CREATE TABLE t (id INTEGER, status TEXT, recipient INTEGER, sender INTEGER);

INSERT INTO t
VALUES
(1, 'ACCEPTED', 2, 1),
(2, 'ACCEPTED', 3, 1),
(3, 'ACCEPTED', 1, 4),
(4, 'REJECTED', 5, 1),
(5, 'REJECTED', 1, 5),
(6, 'ACCEPTED', 6, 7),
(7, 'ACCEPTED', 1, 2);


Query:



SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friends
FROM t
WHERE 1 IN (recipient, sender)
AND status = 'ACCEPTED'
ORDER BY 1


Results:



| friends |
| ------- |
| 2 |
| 3 |
| 4 |


https://www.db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/0



So firstly I've added some more data to prove only the correct data is used. The query checks recipient/sender for 1, as you did in your query. It also checks the status field for 'ACCEPTED', and then it creates a single column using the value which is not 1 (the CASE statement) to the the friend value form the correct column. It then does a DISTINCT to make sure the same person isn't reported twice (if they're a sender in one record and a recipient in another... if that's even possible). The it orders by that generated column.






share|improve this answer





















  • woow, this is great, it works...lemme try it in my code now
    – Oto-obong Eshiett
    Nov 19 at 12:38










  • it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
    – Oto-obong Eshiett
    Nov 29 at 0:04










  • @Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
    – 404
    Nov 29 at 12:30













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',
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%2f53374054%2fselecting-a-list-of-data-which-can-contain-values-from-2-columns%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








up vote
1
down vote



accepted










Setup:



CREATE TABLE t (id INTEGER, status TEXT, recipient INTEGER, sender INTEGER);

INSERT INTO t
VALUES
(1, 'ACCEPTED', 2, 1),
(2, 'ACCEPTED', 3, 1),
(3, 'ACCEPTED', 1, 4),
(4, 'REJECTED', 5, 1),
(5, 'REJECTED', 1, 5),
(6, 'ACCEPTED', 6, 7),
(7, 'ACCEPTED', 1, 2);


Query:



SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friends
FROM t
WHERE 1 IN (recipient, sender)
AND status = 'ACCEPTED'
ORDER BY 1


Results:



| friends |
| ------- |
| 2 |
| 3 |
| 4 |


https://www.db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/0



So firstly I've added some more data to prove only the correct data is used. The query checks recipient/sender for 1, as you did in your query. It also checks the status field for 'ACCEPTED', and then it creates a single column using the value which is not 1 (the CASE statement) to the the friend value form the correct column. It then does a DISTINCT to make sure the same person isn't reported twice (if they're a sender in one record and a recipient in another... if that's even possible). The it orders by that generated column.






share|improve this answer





















  • woow, this is great, it works...lemme try it in my code now
    – Oto-obong Eshiett
    Nov 19 at 12:38










  • it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
    – Oto-obong Eshiett
    Nov 29 at 0:04










  • @Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
    – 404
    Nov 29 at 12:30

















up vote
1
down vote



accepted










Setup:



CREATE TABLE t (id INTEGER, status TEXT, recipient INTEGER, sender INTEGER);

INSERT INTO t
VALUES
(1, 'ACCEPTED', 2, 1),
(2, 'ACCEPTED', 3, 1),
(3, 'ACCEPTED', 1, 4),
(4, 'REJECTED', 5, 1),
(5, 'REJECTED', 1, 5),
(6, 'ACCEPTED', 6, 7),
(7, 'ACCEPTED', 1, 2);


Query:



SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friends
FROM t
WHERE 1 IN (recipient, sender)
AND status = 'ACCEPTED'
ORDER BY 1


Results:



| friends |
| ------- |
| 2 |
| 3 |
| 4 |


https://www.db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/0



So firstly I've added some more data to prove only the correct data is used. The query checks recipient/sender for 1, as you did in your query. It also checks the status field for 'ACCEPTED', and then it creates a single column using the value which is not 1 (the CASE statement) to the the friend value form the correct column. It then does a DISTINCT to make sure the same person isn't reported twice (if they're a sender in one record and a recipient in another... if that's even possible). The it orders by that generated column.






share|improve this answer





















  • woow, this is great, it works...lemme try it in my code now
    – Oto-obong Eshiett
    Nov 19 at 12:38










  • it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
    – Oto-obong Eshiett
    Nov 29 at 0:04










  • @Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
    – 404
    Nov 29 at 12:30















up vote
1
down vote



accepted







up vote
1
down vote



accepted






Setup:



CREATE TABLE t (id INTEGER, status TEXT, recipient INTEGER, sender INTEGER);

INSERT INTO t
VALUES
(1, 'ACCEPTED', 2, 1),
(2, 'ACCEPTED', 3, 1),
(3, 'ACCEPTED', 1, 4),
(4, 'REJECTED', 5, 1),
(5, 'REJECTED', 1, 5),
(6, 'ACCEPTED', 6, 7),
(7, 'ACCEPTED', 1, 2);


Query:



SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friends
FROM t
WHERE 1 IN (recipient, sender)
AND status = 'ACCEPTED'
ORDER BY 1


Results:



| friends |
| ------- |
| 2 |
| 3 |
| 4 |


https://www.db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/0



So firstly I've added some more data to prove only the correct data is used. The query checks recipient/sender for 1, as you did in your query. It also checks the status field for 'ACCEPTED', and then it creates a single column using the value which is not 1 (the CASE statement) to the the friend value form the correct column. It then does a DISTINCT to make sure the same person isn't reported twice (if they're a sender in one record and a recipient in another... if that's even possible). The it orders by that generated column.






share|improve this answer












Setup:



CREATE TABLE t (id INTEGER, status TEXT, recipient INTEGER, sender INTEGER);

INSERT INTO t
VALUES
(1, 'ACCEPTED', 2, 1),
(2, 'ACCEPTED', 3, 1),
(3, 'ACCEPTED', 1, 4),
(4, 'REJECTED', 5, 1),
(5, 'REJECTED', 1, 5),
(6, 'ACCEPTED', 6, 7),
(7, 'ACCEPTED', 1, 2);


Query:



SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friends
FROM t
WHERE 1 IN (recipient, sender)
AND status = 'ACCEPTED'
ORDER BY 1


Results:



| friends |
| ------- |
| 2 |
| 3 |
| 4 |


https://www.db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/0



So firstly I've added some more data to prove only the correct data is used. The query checks recipient/sender for 1, as you did in your query. It also checks the status field for 'ACCEPTED', and then it creates a single column using the value which is not 1 (the CASE statement) to the the friend value form the correct column. It then does a DISTINCT to make sure the same person isn't reported twice (if they're a sender in one record and a recipient in another... if that's even possible). The it orders by that generated column.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:31









404

3,0351626




3,0351626












  • woow, this is great, it works...lemme try it in my code now
    – Oto-obong Eshiett
    Nov 19 at 12:38










  • it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
    – Oto-obong Eshiett
    Nov 29 at 0:04










  • @Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
    – 404
    Nov 29 at 12:30




















  • woow, this is great, it works...lemme try it in my code now
    – Oto-obong Eshiett
    Nov 19 at 12:38










  • it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
    – Oto-obong Eshiett
    Nov 29 at 0:04










  • @Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
    – 404
    Nov 29 at 12:30


















woow, this is great, it works...lemme try it in my code now
– Oto-obong Eshiett
Nov 19 at 12:38




woow, this is great, it works...lemme try it in my code now
– Oto-obong Eshiett
Nov 19 at 12:38












it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
– Oto-obong Eshiett
Nov 29 at 0:04




it works sir, but what if i want to go the extra mile . Imagine we have a users table which contains Firstname, lastname and email and i want to get the Users from the Users Table based on the IDs i have in the friends row . so instead of me just getting a column of id,the query takes the id's and finds the User in the Users table ?
– Oto-obong Eshiett
Nov 29 at 0:04












@Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
– 404
Nov 29 at 12:30






@Oto-obongEshiett You'll need to join the users table on the results of this query. See db-fiddle.com/f/sfbFeXCvWkoSagJEipfxSr/1 for an example.
– 404
Nov 29 at 12:30




















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%2f53374054%2fselecting-a-list-of-data-which-can-contain-values-from-2-columns%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy