Get all none contact from a user sql - mysql

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
);

Related

Get mutual relationship from database table

I have table user_follow which has only two columns: who and whom. They represent links between users when WHO follows WHOM. If two users follow each other, they are considered friends. In such case the table would contain records:
WHO WHOM
1 2
2 1
Where 1 and 2 are simply user IDs.
In order to determine if two users are friends, I have to query the table and use simple condition
SELECT COUNT(*)
FROM user_follow
WHERE (who = 1 AND whom = 2) OR (who = 2 AND whom = 1)
If I get 2 then they are friends.
But if I want to load list of all user's friends, I cannot do it that way. So i came up with sql join into itself:
SELECT uf2.whom
FROM user_follow AS uf1
LEFT JOIN user_follow AS uf2 ON uf1.who = uf2.whom
WHERE uf1.whom = ? AND uf2.who = ? ORDER BY uf2.whom
I made a dummy table to test it and it works. But I would like for someone to confirm this is the correct solution.
I think your query is correct if you supply the same parameter value for ?. I find the query below simpler to understand.
The query below lists the friends (users who follow each other) of a user:
SELECT uf1.whom
FROM user_follow AS uf1
INNER JOIN user_follow AS uf2
-- filter down to followed users
ON uf1.whom = uf2.who
-- followed users follow their followers
AND uf1.who = uf2.whom
-- the user whose friends are listed
WHERE uf1.who = ?
ORDER BY 1
;

Query to check relationships between columns?

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 = ?
)

Two tables and join and left join together

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;

Using select statement with two tables

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

Getting a list of data based on Items associated with User

I have a table called reviews. I get the most current user reviews like this:
SELECT b.item, b.item_id, a.review_id, a.review, c.category, u.username, c.cat_id
FROM reviews a
INNER JOIN items b
ON a.item_id = b.item_id
INNER JOIN master_cat c
ON c.cat_id = b.cat_id
INNER JOIN users AS u
ON u.user_id = a.user_id
ORDER BY a.review_id DESC;
What I want to do is slightly alter it to be more personable for users.
I have another table of user "connections". Kind of like Twitter. When a user follows someone, it gets logged in this table called profile_follow. This has three columns. id, user_id, follow_id. Simply: If I am user #1, and I "follow" user # 3 and user #5, two rows will be added in this table:
profile_follow
------------------------
id | user_id | follow_id
| 1 | 3
| 1 | 5
Here is how I want to change the query above. I want to only show newest reviews, from people you follow.
So I will need at least one more join, for table profile_follow. And I need to pass in a user_id (it's a php function), doing something like `WHERE profile_follow.user_id = '{$user_id}'. I think I will have to add a sub query on this, not use.
Can someone show me how to finish this query? I am not sure how to handle it from here? All of my attempts have been off so far.
I think I need to do something like:
Selectfollow_idwhereuser_id= (logged in user)
And then in the main query:
Select reviews only with profile_follow.follow_id = review.user_id.
I can't figure out how to make this filter work.
Always difficult without testing, but:
SELECT b.item, b.item_id, a.review_id, a.review, c.category, u.username, c.cat_id
FROM reviews a
INNER JOIN items b
ON a.item_id = b.item_id
INNER JOIN master_cat c
ON c.cat_id = b.cat_id
INNER JOIN profile_follow pf
ON pf.follow_id = a.user_id
WHERE profile_follow.user_id = '{$user_id}'
ORDER BY a.review_id DESC;