码迷,mamicode.com
首页 > 其他好文 > 详细

where EXISTS (子查询)多对多中通过中间表查对方列表

时间:2018-04-20 16:07:33      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:ati   返回   记录   from   sts   font   select查询   col   exist   

用户表A,小组表B,小组和用户是多对多关系,中间有个中间表M

已知 小组 id 即teamId ,想知道这个小组中的用户列表信息,可以如下写sql:

 

select * from A a where EXISTS (select m.id from M m where a.id = m.aid and m.bid = (参数teamId));

where 后面 跟 exists (子查询)的意思是,括号里面子查询至少有一条记录时,前面的大的select查询语句才返回结果

 

此查询语句中涉及了三张表:表A ,表B, AB多对多关系表M

【记忆方法:因为要查用户列表,所以外层的大select 查询的from是用户表,然后后面子查询中才是A和B与中间表中的关系条件】

 

where EXISTS (子查询)多对多中通过中间表查对方列表

标签:ati   返回   记录   from   sts   font   select查询   col   exist   

原文地址:https://www.cnblogs.com/libin6505/p/8890897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!