如上图。有两个表,表一是所有帐户,表二是某个领导下属的人。
我想表一里面检索一个account列表。其中传入参数是account,首先从表二里面选择出来一个authority列表,比如account传入参数是李四,那么select authority from 表二 where account='李四',即出来这个字符串:“王二*麻子*刘大爷”,另外这个结果是唯一的!然后从表一里面查询出来所有的account结果,条件是account不在刚才查出来的字符串里面,就这个要求。
那么如果就像上面所说的,传入参数是李四的话,那么结果是:
张三
王太婆
即结果不在“王二*麻子*刘大爷”里面,当然了,同时不能等于传入参数。我这样做不对:
select account from 表一 where charindex(account,(select authority from 表二) where account='李四')=0

解决方案 »

  1.   

    select account from 表一 
        where charindex(account+'*',(select authority+'*李四*' 
                                 from 表二 where account='李四'))=0
      

  2.   

    declare @s varchar(100)select @s = authority from 表二 where account='李四'
    select * 
    from 表一 as a
    where charindex('*'+account+'*','*'+@s+'*'+'*李四*') = 0 and  
      

  3.   

    select *  
    from 表一 as a
    where charindex('*'+account+'*','*'+@s+'*李四*') = 0
      

  4.   

    select charindex('王二','王二*麻子*刘大爷')
    select account from 表一 
    where charindex(account,(select authority from 表二 where account='李四'))=0
    and   account<>'李四'