I currently have a table called User_Followers this table has the following columns:
User_Id
Follower_Id
I'm trying to build a query that will retrieve all the people a user is following but no being followed back by.
For example:
User ID | Follower ID
1 2
1 4
3 1
4 2
4 3
In the example above, if I was to check for users not following back User 4. It should return 1.
Similarly if I was to check for users not following back User 2, it should return 1 and 4
I'm trying to build a query that will retrieve all the people a user is following but no being followed back by.
If I understand correctly, then not exists seems to do what you want:
select uf.user_id
from User_Followers uf
where uf.follower_id = #user and
not exists (select 1
from User_Followers uf2
where uf2.user_id = uf.follower_id and
uf2.follower_id = uf.user_id
);
You have 2 queries :
"being followed" can be written this way :
SELECT u.Follower_Id
FROM User_Followers u
WHERE u.User_Id = ?
"all the people a user is following" can be done this way :
SELECT u.User_Id
FROM User_Followers u
WHERE u.Follower_Id = ?
Now, you can combine both queries to perform the "all the people a user is following" and not "being followed"
SELECT u.User_Id
FROM User_Followers u
WHERE u.Follower_Id = ?
AND u.User_Id NOT IN
(
SELECT uf.Follower_Id
FROM User_Followers uf
WHERE uf.User_Id = ?
)
Related
I hope you are well :).
I'm a little bit stuck with a problem in sql that I can't solve. I would like to get all the people who are not yet friends with a user without tuple.
That is to say that in my column user_id or contact_user_id, it would be necessary that if: user_id = 1 and contact_user_id = 2 (they are friends) that it does not show for the user 2 that it is not friends with user 1.
It should display for user 2 that he is missing user 3,4 and not 1 nor himself
I have for the moment a request of the kind but it does not display the good expected result.
Thanks a lot to the person who will take the time to answer
Their is the desired result without the first line (cause their are already friend) :
I got for the moment this sql statement :
select us.user_id, us.login
from user us where u.user_id!=2
and not exists (select 1 from contact ctc
where ctc.contact_user_id = us.user_id
and ctc.user_id = 2);
EDIT : I maybe founded the sql request
select u.*
from user u
where not exists (select 1
from contact c
where (c.user_id = u.user_id and c.contact_user_id = 2) or (c.contact_user_id= u.user_id and c.user_id=2)
);
If you want records where there is no reciprocal relationship (as your description implies), you can use not exists:
select c.*
from contact c
where not exists (select 1
from contact c2
where c2.contact_user_id = c.user_id and
c.user_id = c.contact_user_id
);
If you want additional information from another table, just join in the outer query:
select c.*
from contact c join
user u
on c.user_id = u.user_id
where not exists (select 1
from contact c2
where c2.contact_user_id = c.user_id and
c.user_id = c.contact_user_id
);
I have a users table and user_followings table. The tables have the basic structure:
users: id, name, email
users_followings: following_user_id, follower_user_id
follower_user_id is someone who is following some other person.
following_user_id is someone who is being followed by some other
person
I want that one can click on a particular user to see all the information like who are following him/her and who are the people that he/she is follwing.
SELECT
users.id,
users.name,
users.email
from users
JOIN user_followings ON
user_followings.follower_user_id = users.id
WHERE user_followings.following_user_id = 1
This query basically joins two table and fetches desired result.
Now suppose a user named 'A' is logged in and he is looking at user X's profile. There are many people who have followed user X.
Let's say John, Mike, Rusev, Jack etc
How can write a query that tells whether logged in User 'A' is following John, Mike, Rusev, Jack etc or not along with the query that is above there.
So user A should be able to know whether he is following John, Mike, Rusev, Jack etc or not
My understanding is that OP wants to see what users are following the current user (A) that also follows the user A is viewing (X)
In my example A is id = 1 and X is id = 6
SELECT fu.id, fu.name, fu.email
FROM users u
JOIN users_followings f ON f.userId = u.id
JOIN users fu on fu.id = f.follower
WHERE f.userId = 1
AND f.follower IN (SELECT follower
FROM users_followings
WHERE userId = 6)
I changed follower_user_id to follower and following_user_id to userId to not confuse myself
Supposed the user with id=1 is viewing the details of the user with id=2 and you want to the user with id=1 to know if the followings or followers of user with id=2 are related with user with id=1 in any way. Try this:
SELECT C.*,
(SELECT 1 FROM user_followings D WHERE D.following_user_id=1 AND
C.id=D.follower_user_id LIMIT 1) flwx_viewing_user,
(SELECT 1 FROM user_followings E WHERE E.follower_user_id=1 AND
C.id=E.following_user_id LIMIT 1) viewing_user_flwx
FROM
(SELECT A.id, A.name, A.email, 'following' relation
FROM users
WHERE EXIST (SELECT 1
FROM user_followings B
WHERE B.following_user_id=2)
UNION ALL
SELECT A.id, A.name, A.email, 'followers' relation
FROM users
WHERE EXIST (SELECT 1
FROM user_followings B
WHERE B.follower_user_id=2)) C;
I'm not sure I get it right but given ID=1 for A and ID=5 for X.
This query returns for every user that follows X the info if it is followed by A
SELECT
*,
CASE WHEN exists(
SELECT *
FROM following AFOLLOW
WHERE AFOLLOW.follower_user_id = 1
AND XFOLLOWED.follower_user_id = AFOLLOW.following_user_id)
THEN 'FOLLOWING'
ELSE 'NOTFOLLOWING' END
FROM following XFOLLOWED
WHERE following_user_id = 5
AND follower_user_id <> 1;
I have two tables. One contains User and company relationship a show below
User_company
UserId CompanyId
1 2
2 1
3 1
4 2
Another table holds user information
User
Id Name City
1 Peter LA
2 Harry SF
3 John NY
4 Joe CI
How do I make a statement which will give me All the users which are in company 1? Will something like
Select * from User where Id in (Select UserId from User_company where CompanyId = 1)
work?
SELECT * from User
left join User_company on User_company.UserId=User.Id
This would work...
SELECT * works but can be sluggish over time as it may not scale well with more data.
FROM User
WHERE Id in (Select UserId from User_company where CompanyId = 1)
So would this.. - best if you need data from both tables.
SELECT *
FROM User U
INNER JOIN User_Company UC
ON U.ID = UC.UserID
WHERE UC.CompanyID = 1
As would this - Probably the fastest if you just need data from user table.
Select * from User U
where exists (Select * from User_Company UC where U.ID = UC.UserID and CompanyID = 1)
OUTER joins are only needed if you need all records from one table and only those that match in another.
As to which is the best above: it depends on existing indexes and other requirements. Any of the above will return what's been asked for.
Try this
Select u.*
from User u
inner join User_company uc
on u.Id = uc.UserId
and uc.CompanyId = 1
BTW, what's wrong with the query you have posted? It will work as well fine. Just that it's a subquery and you better replace it with Join for performance.
Select * from User where Id in
(Select UserId from User_company where CompanyId = 1)
SELECT U.* FROM User AS U LEFT JOIN
User_company AS UC ON U.Id = UC.UserId WHERE UC.CompanyId = 1
want mysql query for finding mutual friend between two friend but
I am maintain the friendship of user in one way relationship for ex.
first is users table
id name
1 abc
2 xyz
3 pqr
Now second table is friend
id user_id friend_id
1 1 2
2 1 3
3 2 3
Now here i can say that abc(id=1) is friend of xyz(id=2) now similar way the xyz is friend of abc but now i want to find mutual friend between abc(id=1) and xyz(id=2) that is pqr so I want mysql query for that.
REVISED
This query will consider the "one way" relationship of a row in the friend table to be a "two way" relationship. That is, it will consider a friend relationship: ('abc','xyz') to be equivalent to the inverse relationship: ('xyz','abc'). (NOTE: we don't have any guarantee that both rows won't appear in the table, so we need to be careful about that. The UNION operator conveniently eliminates duplicates for us.)
This query should satisfy the specification:
SELECT mf.id
, mf.name
FROM (
SELECT fr.user_id AS user_id
, fr.friend_id AS friend_id
FROM friend fr
JOIN users fru
ON fru.id = fr.user_id
WHERE fru.name IN ('abc','xyz')
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
JOIN users flf
ON flf.id = fl.friend_id
WHERE flf.user IN ('abc','xyz')
) f
JOIN users mf
ON mf.id = f.friend_id
GROUP BY mf.id, mf.name
HAVING COUNT(1) = 2
ORDER BY mf.id, mf.name
SQL Fiddle here http://sqlfiddle.com/#!2/b23a5/2
A more detailed explanation of how we arrive at this is given below. The original queries below assumed that a row in the friend table represented a "one way" relationship, in that "'abc' ff 'xyz'" did not imply "'xyz' ff 'abc'". But additional comments from the OP hinted that this was not the case.
If there is a unique constraint on friend(user_id,friend_id), then one way to get the result would be to get all of the friends of each user, and get a count of rows for that friend. If the count is 2, then we know a particular friend_id appears for both user 'abc' and for 'xyz'
SELECT mf.id
, mf.name
FROM friend f
JOIN users uu
ON uu.id = f.user_id
JOIN users mf
ON mf.id = f.friend_id
WHERE uu.name IN ('abc','xyz')
GROUP BY mf.id, mf.name
HAVING COUNT(1) = 2
ORDER BY mf.id, mf.name
(This approach can also be extended to find a mutual friend of three or more users, by including more users in the IN list, and changing the value we compare the COUNT(1) to.
This isn't the only query that will return the specified resultset; there are other ways to get it as well.
Another way to get an equivalent result:
SELECT u.id
, u.name
FROM ( SELECT f1.friend_id
FROM friend f1
JOIN users u1
ON u1.id = f1.user_id
WHERE u1.name = 'abc'
) t1
JOIN ( SELECT f2.friend_id
FROM friend f2
JOIN users u2
ON u2.id = f2.user_id
WHERE u2.name = 'xyz'
) t2
ON t2.friend_id = t1.friend_id
JOIN users u
ON u.id = t1.friend_id
ORDER BY u.id, u.name
NOTES
These queries do not check whether user 'abc' is a friend of 'xyz' (the two user names specified in the WHERE clause). It is only finding the common friend of both 'abc' and 'xyz'.
FOLLOWUP
The queries above satisfy the specified requirements, and all the examples and test cases provided in the question.
Now it sounds as if you want a row in that relationship table to be considered a "two way" relationship rather than just a "one way" relationship. It sounds like you want to want to consider the friend relationship ('abc','xyz') equivalent to ('xyz','abc').
To get that, then all that needs to be done is to have the query create the inverse rows,, and that makes it easier to query. We just need to be careful that if both those rows ('abc','xyz') and ('xyz','abc') already exist, that we don't create duplicates of them when we invert them.
To create the inverse rows, we can use a query like this. (It's simpler to look at this when we don't have the JOIN to the users table, and we use just the id value:
SELECT fr.user_id
, fr.friend_id
FROM friend fr
WHERE fr.user_id IN (1,2)
UNION
SELECT fl.friend_id AS user_id
, fl.user_id AS friend_id
FROM friend fl
WHERE fl.friend_id IN (1,2)
It's simpler if we don't include the predicates on the user_id and friend_id table, but that could be a very large (and expensive) rowset to materialize.
try this:
given that you want to get the mutual friends of friends 1 & 2
select friend_id into #tbl1 from users where user_id = 1
select friend_id into #tbl2 from users where friend_id = 2
select id, name from users where id in(select friend_id from #tbl1 f1, #tbl2 f2 where f1.friend_id=f2.friend_id)
I'm fairly new to mysql and I have no idea if I'm heading in the right direction but I'm having trouble with a mysql query.
I basically have a table of users
id name
---- --------
1 user1
2 user2
3 user3
4 user4
as well as a table of user attributes
id userid attribute
---- ----- ------
1 1 5
2 1 6
3 2 5
4 3 4
I want to be able to select users that have both the attribute 5 and the attribute 6, so in this case I want to return
id name
---- --------
1 user1
I tried using a join like this.
SELECT u.id, u.name FROM users u LEFT JOIN attributes a ON (a.userid = u.id) WHERE a.attribute = 5 AND a.attribute = 6
But obviously that won't work, what is the best way of doing this?
One way to do this would be to use two joins; eg:
SELECT ...
FROM users u
JOIN attributes a5 ON u.id = a5.userid AND a5.attribute = 5
JOIN attributes a6 ON u.id = a6.userid AND a6.attribute = 6
Another way is by grouping (note that I am a MS SQL person, not sure if this is the right syntax for mysql or not):
SELECT u.id, u.name
FROM users u
JOIN attributes a ON u.id = a.userid
WHERE a.attribute IN (5,6)
GROUP BY u.id, u.name
HAVING COUNT(*) = 2
SELECT u.id, u.name FROM users u
INNER JOIN attributes a1 ON u.id = a1.userid
INNER JOIN attributes a2 ON u.id = a2.userid
WHERE a1.attribute = 5 AND a2.attribute = 6
Based on your question, I don't think the other two current answers are satisfactory.
If you want to:
select users that have both the
attribute 5 and the attribute 6
The following query is one way to accomplish that:
select
*
from
users
where
id in (select userid from attributes where attribute = 5)
and
id in (select userid from attributes where attribute = 6)
Hmm, I am not much into SQL, maybe GROUP BY and HAVING will help you:)
Check out the reference: http://www.w3schools.com/sql/sql_having.asp
Changing the query like this would work:
Select all from attributes table that equals 5 or 6 and then check the users who match.
SELECT a.id, u.name
FROM attributes AS a
LEFT JOIN users AS u ON a.id = u.id
WHERE a.attribute = 5 OR a.attribute = 6