Get those who didnt match











up vote
0
down vote

favorite
1












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










share|improve this question




















  • 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















up vote
0
down vote

favorite
1












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










share|improve this question




















  • 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













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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






share|improve this answer























  • 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











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%2f53251559%2fget-those-who-didnt-match%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










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






share|improve this answer























  • 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















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






share|improve this answer























  • 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















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%2f53251559%2fget-those-who-didnt-match%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