select  name from TableA a join users u on u.id=a.userid where repid <>null  ParkingUserId=1  本身就有错de 

解决方案 »

  1.   

    if 沒意義 reqid is not null改改
      

  2.   


    select name from TableA a join users u 
    on u.id=case state when 1 then a.userid else a.repid end
    where repid <>null and ParkingUserId=1 
      

  3.   


    --> 测试数据: @tableA
    declare @tableA table (id int,userid int,repid int,state int)
    insert into @tableA
    select 1,1,null,1 union all
    select 2,2,1,0 union all
    select 3,3,null,1 union all
    select 4,4,1,0
    --> 测试数据: @users
    declare @users table (id int,name varchar(4))
    insert into @users
    select 1,'mama' union all
    select 2,'baba'
    select name from @TableA a join @users u 
    on u.id=case state when 1 then a.userid else a.repid end
    where repid is not null
      

  4.   

    <> null 是错的
    is not null
      

  5.   

    木看懂.
    select  name from TableA a join users u on u.id=(case when @i=1 then a.userid else a.repid end) where  reqid is not null and ParkingUserId=1 
    ?
      

  6.   

      这个试试,同意楼上的
      select  name from TableA a join users u on u.id=(case when @i=1 then a.userid else a.repid end) where  reqid is not null and ParkingUserId=1