2016-11-26 5 views
0

は、ユーザー表からSQLクエリ接続されていないユーザーの一覧を取得する

例えば、ユーザーID 1を接続していないどのように一覧表示することが接続要求&を送信しました4 & 6.したがって、userid(1)に接続されていない他のユーザーを取得するためのクエリを書く方法。

期待される結果は、答えが高く評価されて

2  bb  [email protected] 
3  cc  [email protected] 
5  ff 

1 =ユーザーIDになります。

答えて

1

簡単なwoulbがあること:

SELECT * from users a 
WHERE userid != :userid 
    AND not exists (select * from connections 
        where (userid = :userid and c_userid = a.userid) 
        or (userid = a.userid and c_userid = :userid)) 
+0

このクエリを試してみてください – Anand

0

以下は、接続テーブルにないすべてのユーザーを返します。クエリ以下

SELECT * FROM users AS u 
    WHERE (SELECT count(*) FROM connections AS c 
     WHERE c.userid=u.userid AND reqType != 0) = 0; 
0

ユーザーID = 1

select u.userid,u.name,u.email 
from connections 
left join users as u on connections.c_userid = u.userid 
where connections.userid =1 and connections.reqtype in (0,1) 
0

に関連付けられていないユーザーを取得しますが、これは魔法のように動作

select *from users where userid not in (select c_userId from connenctions where where userid=1) and userid!=1 
関連する問題