小弟在做一个web模糊查询,写一个sql模糊查询语句,遇到这个棘手问题,请大侠指点。
     在页面上有多个复选框,代表不同的职务,例如:国家主席、军委主席、中央委员、人大代表等,这些职务好无关系啊,一个人可以同时具有多个职务,例如一个人即是国家主席,又是居委主席,这就是胡哥了。
     现在要对具有这些职务的人进行查询,比如查询是人大代表的人,那我们可以这么写:select * from users u where 人大代表 in (select r.职务 from 职务关系表 r where u.id = r.userid);
     那么如果选择两项和多项时,要找即是人大代表又是中央委员的人时,怎么写,请各位为小弟指点一下秘境,不胜感激。
           select * from users u where (人大代表和中央委员) in (select r.职务 from 职务关系表 r where u.id = r.userid);

解决方案 »

  1.   


    select * from users u where u.id=(select r.userid from 职务关系表 r where r.职务='国家主席' or r.职务='军委主席')
     
      

  2.   

    WHERE 职务 IN ('国家主席','军委主席',...)
      

  3.   

    select * from users where uid in (
      select userid from 职务关系表 r where r.职务 in ('国家主席','军委主席')
    )这个就凑合着用吧
      

  4.   


    select u.* from users u where u.id in (
        select userid from 职务关系表 r where r.职务 in ('国家主席','军委主席')  
        group by userid  having count(*) = 2)