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
--> 测试数据: @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
<> null 是错的 is not null
木看懂. 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 ?
这个试试,同意楼上的 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
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
--> 测试数据: @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
is not null
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
?
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