2

I am trying to make an application that will display every other user that is not the logged in user or that the logged in user does not not follow (like an explore page), but I'm having trouble writing the SQL query.

My tables are set up as:

following(username1, username2)

(meaning username1 follows username2)

and

users(username, fullname, ...)

Right now I am trying to do something like:

SELECT * FROM
users u 
LEFT JOIN following f ON (
  u.username != 'loggedInUser'
  AND f.username1 = 'loggedInUser'
  AND f.username2 = u.username
)

I replace loggedInUser in a Python script.

Unfortunately, this query is currently returning all of my users, and it should not be. I'm having a lot of issues working though this logic for some reason, does anyone have some insight?

2
  • 1
    sample data and its output is helpful Commented Jan 30, 2019 at 6:11
  • Learn what left join returns: inner join rows plus unmatched left table rows extended by nulls. Always know what inner join you want as part of a left join. Commented Jan 31, 2019 at 4:20

4 Answers 4

1

This will display every other users that are not the logged in user and (not or) that the logged in user does not not follow (them):

SELECT * 
FROM users u 
WHERE u.username <> 'loggedInUser'
    AND NOT EXISTS (
            SELECT 1
            FROM following f
            WHERE f.username1 = 'loggedInUser'
                AND f.username2 = u.username
        );

Your query could change to an INNER JOIN to get expected result but it may be more complicated with worse performance:

SELECT DISTINCT u.* 
FROM users u 
INNER JOIN following f 
ON f.username1 <> 'loggedInUser'
    OR f.username2 <> u.username
WHERE u.username <> 'loggedInUser';
Sign up to request clarification or add additional context in comments.

Comments

1

If you need to get all the users not followed by the logged in user, just try the below script.

SELECT * FROM
users u 
where u.username in(select username2 from following where username1!= 'loggedInUser')
and u.username!='loggedInUser'

Comments

0
SELECT * FROM
users u 
LEFT JOIN following f ON u.[insert_primary_key_here]=f.[insert_foreign_key_here] 
WHERE u.username != 'loggedInUser'
AND f.username1 = 'loggedInUser'
AND f.username2 = u.username

Comments

-1

Why don't you simply create a new column in users of bit type that will be updated when any user is being logged in . Then you just have to write a simply query that would be

Select * from users where loggedIn=1

that will return you all the users data with logged in status

2 Comments

This doesn't seem to address the main point of the question which is to include the users linked in the following table.
I have explained her another way of doing work and it could be helpful for her . There is multiple ways of doing the same thing and I have explained her the easy and effective way.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.