现有两个表,t_corp_user 和 t_co_user,表t_corp_user中有一个用户(字段reg_sim)对应多个管理员(sq_co_sim),反之也一样,表sq_co_sim中有一个操作员(reg_sim)对应一个管理员(login_sim),但一个管理员对应多个操作员。用户和操作员没有直接的对应关系。
用户---管理员---操作员
已知   用户(10023718700)和另一个有可能为管理员或操作员(10003710700),我想看看这个用户是不是属于这个管理员或操作员。
!!如太麻烦请直接看下面的语句
select id,reg_sim from t_corp_user 
where reg_sim='10023718700' and (sq_co_sim='10003710700' or '10003710700' in (select reg_sim from t_co_user where login_sim in (select sq_co_sim from t_corp_user where reg_sim='10023718700')))

解决方案 »

  1.   

    where reg_sim='10023718700' and (sq_co_sim='10003710700' or '10003710700' in 有问题。
    where reg_sim='10023718700' and sq_co_sim='10003710700' or sq_co_sim='10003710700' or sq_co_sim in (
      

  2.   

    你得逻辑上可能没有问题,但是写的可能比较烦,还有这样一个sql中用到一个表两次,已经两个表有同样的列名需要对表起别名,并表明该列属于那张表!你看我写的这个是否可以!?献丑了
    select tc.id,tc.reg_sim from t_corp_user tc
    where tc.reg_sim='10023718700' 
    and (tc.sq_co_sim='10003710700' 
    or exists (select null 
    from t_co_user tcop,t_corp_user tcor 
    where tcop.login_sim = tcor.sq_co_sim 
    and tcor.reg_sim = '10023718700'
    and tcop.reg_sim = '10003710700'))