Pregunta

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

¿Fue útil?

Solución

Try this

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

Otros consejos

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top