문제

I have 2 tables , Users and Friends Table, Users have uID, Friends have UID as well, I want to check the UIDs present in Users table but not present in Friends Table, I am using this query

SELECT b.userId 
FROM users b 
where b.userId NOT IN(
  select userId 
  from user_friend
)

But it is returning me all the rows of Users Table.. Am I missing something? Example Data in MYSQL

Users Table
1
2
3
4
Friends Table
2
3
4

I actually need to get 1

도움이 되었습니까?

해결책

Try this

SELECT b.userId 
FROM users b 
WHERE NOT EXISTS (
   SELECT * 
   FROM user_friend
   WHERE b.userId = user_friend.userId
);

다른 팁

Try this one:

SELECT b.userId 
FROM users b 
where b.userId NOT IN(
  select user_friend.userId 
  from user_friend
)

A Work Around for your query

SELECT  U.UserId
FROM    Users U
EXCEPT
SELECT  F.UserId
FROM    Friends F
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top