一对多,在多的一方设查询条件

来源:互联网 发布:小白微信墙源码 编辑:程序博客网 时间:2024/04/30 00:31

     需求是这样:一个企业可以有多个资质信息,查询用户选择的资质信息的企业。图如下:

 

sql语句如下,SELECT   * from   T_Enterprise_BaseInfo   where   exists
    ( select distinct EPID from   T_EntBI_EntQ  e   where
   exists (select 1 from   T_EntBI_EntQ  f  where QID = 1  and  a.EPID = f.EPID )
   and exists (select 1 from   T_EntBI_EntQ  g  where QID = 2  and  a.EPID = g.EPID )
   and exists (select 1 from   T_EntBI_EntQ  h  where QID = 5  and  a.EPID = h.EPID )
     )

一个 T_EntBI_EntQ  表中一个企业可以有多个资质,如果当前epid可以查到这些资质就说明这个epid满足条件。

原创粉丝点击