SQL查询以获取未连接的用户列表

时间:2016-11-26 08:23:57

标签: mysql sql

我有两个名为Users& amp;联系enter image description here

我需要获取用户列表如何没有从用户表连接

例如,用户ID 1已发送请求&连接到4& 6.那么如何编写查询来获取与userid(1)无关的其他用户。

预期结果将是userid = 1

2      bb     b@a.com 
3      cc     c@c.com 
5      ff

答案很感激。

4 个答案:

答案 0 :(得分: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))

答案 1 :(得分:0)

以下内容将返回不在连接表中的所有用户。

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

答案 2 :(得分:0)

以下查询获取与userid = 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)

答案 3 :(得分:0)

您尝试此查询

select *from users where userid not in (select c_userId from connenctions where where userid=1) and userid!=1