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):
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)
sql postgresql
|
show 1 more comment
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):
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)
sql postgresql
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 oftime
, 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
|
show 1 more comment
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):
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)
sql postgresql
I have a friend Request table which holds a friend request data between two users (recipient and sender) and also the friendrequeststatus (ACCEPTED, REJECTED):
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)
sql postgresql
sql postgresql
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 oftime
, 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
|
show 1 more comment
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 oftime
, 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
|
show 1 more comment
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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