Get those who didnt match
up vote
0
down vote
favorite
There are two users
The User 1 profile_id
3734387944
then the User 2 Profile_id
1421536173
there is a table named friends with a structure of
from_profile_id
to_profile_id
request
blocked_by
and this is the data and structure of users
var_id profile_id fullname firstname lastname
180 3734387944 John Louis Domincel John Louis Domincel
181 1421536173 James Domincel James Domincel
and this is the data and structure of friends
from_profile_id to_profile_id request blocked_by
3734387944 1421536173 3 3734387944
and this is the data and structure of post
id profile_id text
1 3734387944 POST OF USER 1
2 1421536173 POST OF USER 2
Using this Query, I can select all post of every user
SELECT * FROM post ORDER BY var_id DESC
But i want to filter it:
User 1 has blocked user 2 so both of them cannot receive each user post, Same Function. If not friends, But if they are friends the data will show up in each user.
Request Legend
1 = Friend Request
2 = Friends
3 = Blocked
I have tried this Another Query: But User 1 can see the user 2 posts while user 2 cannot see User 1 post if blocked only. I want to
modify it and make the two of them cannot see each user posts if Blocked and not Friends
SELECT u.*,f.*
FROM post AS u
LEFT JOIN friends AS f
ON f.blocked_by = u.profile_id AND
f.blocked_by <> "3734387944" AND
(
f.to_profile_id = "3734387944" OR
f.from_profile_id = "3734387944"
) AND
f.request = 3
WHERE f.from_profile_id IS NULL
Here's a DB-FIDDLE LINK
mysql sql
|
show 1 more comment
up vote
0
down vote
favorite
There are two users
The User 1 profile_id
3734387944
then the User 2 Profile_id
1421536173
there is a table named friends with a structure of
from_profile_id
to_profile_id
request
blocked_by
and this is the data and structure of users
var_id profile_id fullname firstname lastname
180 3734387944 John Louis Domincel John Louis Domincel
181 1421536173 James Domincel James Domincel
and this is the data and structure of friends
from_profile_id to_profile_id request blocked_by
3734387944 1421536173 3 3734387944
and this is the data and structure of post
id profile_id text
1 3734387944 POST OF USER 1
2 1421536173 POST OF USER 2
Using this Query, I can select all post of every user
SELECT * FROM post ORDER BY var_id DESC
But i want to filter it:
User 1 has blocked user 2 so both of them cannot receive each user post, Same Function. If not friends, But if they are friends the data will show up in each user.
Request Legend
1 = Friend Request
2 = Friends
3 = Blocked
I have tried this Another Query: But User 1 can see the user 2 posts while user 2 cannot see User 1 post if blocked only. I want to
modify it and make the two of them cannot see each user posts if Blocked and not Friends
SELECT u.*,f.*
FROM post AS u
LEFT JOIN friends AS f
ON f.blocked_by = u.profile_id AND
f.blocked_by <> "3734387944" AND
(
f.to_profile_id = "3734387944" OR
f.from_profile_id = "3734387944"
) AND
f.request = 3
WHERE f.from_profile_id IS NULL
Here's a DB-FIDDLE LINK
mysql sql
1
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
1
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
There are two users
The User 1 profile_id
3734387944
then the User 2 Profile_id
1421536173
there is a table named friends with a structure of
from_profile_id
to_profile_id
request
blocked_by
and this is the data and structure of users
var_id profile_id fullname firstname lastname
180 3734387944 John Louis Domincel John Louis Domincel
181 1421536173 James Domincel James Domincel
and this is the data and structure of friends
from_profile_id to_profile_id request blocked_by
3734387944 1421536173 3 3734387944
and this is the data and structure of post
id profile_id text
1 3734387944 POST OF USER 1
2 1421536173 POST OF USER 2
Using this Query, I can select all post of every user
SELECT * FROM post ORDER BY var_id DESC
But i want to filter it:
User 1 has blocked user 2 so both of them cannot receive each user post, Same Function. If not friends, But if they are friends the data will show up in each user.
Request Legend
1 = Friend Request
2 = Friends
3 = Blocked
I have tried this Another Query: But User 1 can see the user 2 posts while user 2 cannot see User 1 post if blocked only. I want to
modify it and make the two of them cannot see each user posts if Blocked and not Friends
SELECT u.*,f.*
FROM post AS u
LEFT JOIN friends AS f
ON f.blocked_by = u.profile_id AND
f.blocked_by <> "3734387944" AND
(
f.to_profile_id = "3734387944" OR
f.from_profile_id = "3734387944"
) AND
f.request = 3
WHERE f.from_profile_id IS NULL
Here's a DB-FIDDLE LINK
mysql sql
There are two users
The User 1 profile_id
3734387944
then the User 2 Profile_id
1421536173
there is a table named friends with a structure of
from_profile_id
to_profile_id
request
blocked_by
and this is the data and structure of users
var_id profile_id fullname firstname lastname
180 3734387944 John Louis Domincel John Louis Domincel
181 1421536173 James Domincel James Domincel
and this is the data and structure of friends
from_profile_id to_profile_id request blocked_by
3734387944 1421536173 3 3734387944
and this is the data and structure of post
id profile_id text
1 3734387944 POST OF USER 1
2 1421536173 POST OF USER 2
Using this Query, I can select all post of every user
SELECT * FROM post ORDER BY var_id DESC
But i want to filter it:
User 1 has blocked user 2 so both of them cannot receive each user post, Same Function. If not friends, But if they are friends the data will show up in each user.
Request Legend
1 = Friend Request
2 = Friends
3 = Blocked
I have tried this Another Query: But User 1 can see the user 2 posts while user 2 cannot see User 1 post if blocked only. I want to
modify it and make the two of them cannot see each user posts if Blocked and not Friends
SELECT u.*,f.*
FROM post AS u
LEFT JOIN friends AS f
ON f.blocked_by = u.profile_id AND
f.blocked_by <> "3734387944" AND
(
f.to_profile_id = "3734387944" OR
f.from_profile_id = "3734387944"
) AND
f.request = 3
WHERE f.from_profile_id IS NULL
Here's a DB-FIDDLE LINK
mysql sql
mysql sql
edited Nov 11 at 18:58
Lawrence Cherone
33k43675
33k43675
asked Nov 11 at 17:56
John Louis Domincel
637
637
1
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
1
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14
|
show 1 more comment
1
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
1
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14
1
1
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
1
1
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
As mentioned in comment use a CASE statement sub-select to get the correct users id, the rest is then just a normal select.
...
WHERE u.profile_id IN (
SELECT CASE WHEN blocked_by = ? THEN from_profile_id ELSE to_profile_id END AS friend_id
FROM friends
WHERE (from_profile_id = ? OR to_profile_id = ?) AND request = 1
)
...
https://www.db-fiddle.com/f/9K3wy4tqRKVGgvvyxvkgMS/0
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
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
As mentioned in comment use a CASE statement sub-select to get the correct users id, the rest is then just a normal select.
...
WHERE u.profile_id IN (
SELECT CASE WHEN blocked_by = ? THEN from_profile_id ELSE to_profile_id END AS friend_id
FROM friends
WHERE (from_profile_id = ? OR to_profile_id = ?) AND request = 1
)
...
https://www.db-fiddle.com/f/9K3wy4tqRKVGgvvyxvkgMS/0
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
add a comment |
up vote
1
down vote
accepted
As mentioned in comment use a CASE statement sub-select to get the correct users id, the rest is then just a normal select.
...
WHERE u.profile_id IN (
SELECT CASE WHEN blocked_by = ? THEN from_profile_id ELSE to_profile_id END AS friend_id
FROM friends
WHERE (from_profile_id = ? OR to_profile_id = ?) AND request = 1
)
...
https://www.db-fiddle.com/f/9K3wy4tqRKVGgvvyxvkgMS/0
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As mentioned in comment use a CASE statement sub-select to get the correct users id, the rest is then just a normal select.
...
WHERE u.profile_id IN (
SELECT CASE WHEN blocked_by = ? THEN from_profile_id ELSE to_profile_id END AS friend_id
FROM friends
WHERE (from_profile_id = ? OR to_profile_id = ?) AND request = 1
)
...
https://www.db-fiddle.com/f/9K3wy4tqRKVGgvvyxvkgMS/0
As mentioned in comment use a CASE statement sub-select to get the correct users id, the rest is then just a normal select.
...
WHERE u.profile_id IN (
SELECT CASE WHEN blocked_by = ? THEN from_profile_id ELSE to_profile_id END AS friend_id
FROM friends
WHERE (from_profile_id = ? OR to_profile_id = ?) AND request = 1
)
...
https://www.db-fiddle.com/f/9K3wy4tqRKVGgvvyxvkgMS/0
edited Nov 11 at 19:03
answered Nov 11 at 18:57
Lawrence Cherone
33k43675
33k43675
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
add a comment |
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
can i combine it with one query
– John Louis Domincel
Nov 11 at 19:10
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
not that im aware of as you basically got two columns but only one is right to match so you need to find out the correct id to match, whats wrong with a sub select? this is common for social table i.e friends/messages structure where you dont want to have two rows.
– Lawrence Cherone
Nov 11 at 19:31
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%2f53251559%2fget-those-who-didnt-match%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
1
Isn't post_id primary key in the posts table ? You have post_id = 1 twice.
– Madhur Bhaiya
Nov 11 at 18:00
you could simply check if its empty or null if not a sub select with a CASE to get the correct user id for matching.
– Lawrence Cherone
Nov 11 at 18:01
@MadhurBhaiya edited just now
– John Louis Domincel
Nov 11 at 18:04
@JohnLouisDomincel please try to create a db-fiddle.com It helps when lots of tables involved.
– Madhur Bhaiya
Nov 11 at 18:05
1
db-fiddle.com/f/226Wiq4GxXpQqKRnQJFWuy/6
– John Louis Domincel
Nov 11 at 18:14