0

I'm attempting to do a bit of MySQL magic, and I've run into a wall.

Essentially, this will be the code that pulls relevant posts into a user's main "newsfeed" page. However, it isn't pulling posts from friends; it's pulling posts from anyone within the same group in the system, but it should (obviously) not display posts from people that have blocked them. I understand the JOIN process, but I'm having trouble figuring out the right syntax for the latter part.

This is the current query:

SELECT stories.id
FROM stories
JOIN users ON stories.posterID=users.id
JOIN relationships ON relationships.user1=stories.posterID
WHERE users.schoolID='$school'
AND (relationships.rel <> '3' WHERE relationships.user2=stories.posterID)

The "rel" field is simply the flag for the user-to-user "relationship"; in this case, 3 means they've been blocked, though it's possible there won't be a relationship entry between those users at all, for obvious reasons. Is there any way to go about doing this?

2
  • 1
    you need to give us the tables fields Jul 11, 2013 at 20:41
  • Answering this without a schema is close to guessing. Please supply relevant information of the schema and I might be able to help you. Jul 11, 2013 at 20:49

2 Answers 2

1

I think I can see what you are trying to do, give this a try:

SELECT 
  stories.id
FROM 
  stories
INNER JOIN 
  users ON users.id = stories.posterID
INNER JOIN 
  relationships ON relationships.user1 = users.id
WHERE 
  users.schoolID = '$school'
AND
  NOT EXISTS (
    SELECT 
      1 
    FROM 
      relationships AS r 
    WHERE 
      r.user1 = users.id 
    AND 
      r.user2 = stories.posterID 
    AND 
      r.rel = 3 
  )
3
  • An anti join pattern will often perform better than an equivalent query that uses a NOT EXISTS (correlated subquery). The query in this answer seems to exclude any stories for a user that doesn't have at least one corresponding 'user1' value in the relationship table (due to the inner join operation). Jul 11, 2013 at 21:06
  • This query also seems to block all stories for any user that is blocked by any user. If user A blocks user B, then users C and D will also not see any stories from user B. Jul 11, 2013 at 21:13
  • 1
    If this is an answer that fits your question Im quite impressed by @AlexP. Since it is pretty difficult to understand the intended result of a query without knowing the schema. Los of kudos. Jul 11, 2013 at 22:01
0

Yes, it's possible to return the specified result set. Often the most efficient way to return this type of result is using an anti-join pattern.

What seems to be missing is a predicate that identifies the user you are pulling the newsfeed for.

Without that, then any user (A) that blocks another user (B) is going to also block stories from user B for all other users as well... no one will be able to see stories from user B. It seems like a block row in the relationship table is meant for a single user to 'block' stories from another single user.

SELECT s.id
  FROM stories s
  JOIN users u
    ON u.id = s.posterID
  LEFT
  JOIN (
         SELECT r.user2
           FROM relationships r
          WHERE r.rel = '3'
            AND r.user1 = 'me'
       ) b
    ON b.user2 = u.id
 WHERE b.user2 IS NULL
   AND u.schoolID='$school'

Let's break that down a bit, first, eliminating that inline view aliased as b.

This query should return all stories posted by all users with the given schoolID.

SELECT s.id
  FROM stories s
  JOIN users u
    ON u.id = s.posterID
 WHERE u.schoolID='$school'

Now, we want to "block" stories from any users that this user (let's call him 'me') has chosen to block.

I'm going to make an assumption about how the "blocking" works. I'm going to assume that if user1='me' has blocked 'user2', that means that no stories from 'user2' should be presented to 'me'. This will be represented in the relationship table by a row of

(user1='me','user2',3)

If that's right, then that means that this this query:

SELECT r.user2
  FROM relationships r
 WHERE r.rel = '3'
   AND r.user1 = 'me'

Will return a list of all of the users that have been blocked by user 'me'.

The "trick" now is to do a left join of this result with the result from the first query, and then discard any rows where we find a match. This is called an "anti-join" pattern. Note that it's the IS NULL predicate is what eliminates the matching rows.


Followup:

Q: If I wanted to avoid displaying "stories" to both the blocker and the blocked, is there a simple modification I can make...

A: If the "block" row in the relationship table looks like this:

(user1='me',user2,3)

we previously took that to mean that user 'me' had chosen to not see any stories from user2.

If we also want this same row to be interpreted to mean that user 'me' is also choosing that user2 will not be allowed to see stories from 'me', that's another check we need to do, but it's not difficult.

If we go back to the query looked for users that were blocked by 'me', (where 'me' doesn't want to see stories from user2)

SELECT r.user2
  FROM relationships r
 WHERE r.rel = '3'
  AND r.user1 = 'me'

We can just swap the positions of user1 and user2 in that query, like this:

SELECT r.user1
  FROM relationships r
 WHERE r.rel = '3'
  AND r.user2 = 'me'

And that will get a list of the users that have chosen to block 'me'.

To exclude stories from those users, we add that query (as an inline view) to the original query, and do the same anti-join pattern: look for "matching rows", and then exclude any rows that found a match.

In the query below, the new inline view is assigned an alias of d (for no particular reason, other than the 'd' looks like a mirror image of 'b')

SELECT s.id
  FROM stories s
  JOIN users u
    ON u.id = s.posterID
  LEFT
  JOIN (
         SELECT r.user2
           FROM relationships r
          WHERE r.rel = '3'
            AND r.user1 = 'me'
       ) b
    ON b.user2 = u.id
  LEFT
  JOIN (
         SELECT r.user1
           FROM relationships r
          WHERE r.rel = '3'
            AND r.user2 = 'me'
       ) d
    ON d.user1 = u.id
 WHERE b.user2 IS NULL
   AND d.user1 IS NULL
   AND u.schoolID='$school'

NOTE: I haven't tested that, but it seems like that will return the specified resultset, if my understanding of the requirement is sound.

Give that a whirl, and let me know how big of a smoke ball that makes.

1
  • @Sean: I added a followup to my answer. I explained my understanding of the new requirement, and explained how I would go about formulating a query that gets the specified resultset. (It's a simple modification, not a change to the whole structure.) Jul 12, 2013 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.