doudeng9425 2016-01-06 13:07
浏览 8
已采纳

选择不在团队中的所有用户

My database looks something like this, in order of user, userteam (connection table) and team:

    Usertable              UserTeam              Teamtable
+--------+------+    +--------+--------+    +--------+-------+
| userid | name |    | userid | teamid |    | teamid | name  |
+--------+------+    +--------+--------+    +--------+-------+
| 1      | eric |    | 1      |      1 |    |      1 | awsm  |
| 2      | john |    | 1      |      2 |    |      2 | doe   |   
| 3      | carl |    | 2      |      1 |    |      3 | empty |
+--------+------+    | 3      |      1 |    +--------+-------+
                     +--------+--------+

How do I select all users that IS NOT in a team, and echo them out only once? I've tried doing it with the usertable, but then it will echo for instance, if I try and select all members not in team 3:

SELECT userid FROM userteam WHERE teamid!=3;
SELECT * FROM user WHERE userid='$previousSql';

1 eric
1 eric
2 john
3 carl

What I'd like instead is:

1 eric
2 john
3 carl

I'm making an add members function, and I'd very much not like everyone to show up more than once.. Any directions, help or guiding would be much appreciated.

  • 写回答

3条回答 默认 最新

  • duannaxin9975 2016-01-06 13:12
    关注

    This should do it:

    SELECT u.userid, u.name FROM Usertable u
    INNER JOIN UserTeam ut ON ut.userid = u.userid
    INNER JOIN Teamtable tt ON tt.teamid = ut.teamid
    WHERE tt.teamid != 3
    GROUP BY u.userid, u.name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教