表A
id , userids
1 1,2
2 4
3 19表B
useid info
1 xxx
2 xxx
3 xxx怎么读出 表B 在表A的 userids 字段中的记录
例如 当A.id =1 时候读出 B.id =1, B.id=2 的记录我开始想 select * from b where userid in ( select userids from A where A.id=1 )
发现不行以前弄过 太久忘记了
id , userids
1 1,2
2 4
3 19表B
useid info
1 xxx
2 xxx
3 xxx怎么读出 表B 在表A的 userids 字段中的记录
例如 当A.id =1 时候读出 B.id =1, B.id=2 的记录我开始想 select * from b where userid in ( select userids from A where A.id=1 )
发现不行以前弄过 太久忘记了
select a.* from a inner join b on ','+a.userids+',' like '%,'+ltrim(b.userid)+',%'
if object_id('A','U') is not null
drop table A
go
create table A
(
id int,
userids varchar(10)
)
go
insert into A
select 1,'1,2' union all
select 2,'4' union all
select 3,'19'
go
if object_id('B','U') is not null
drop table B
go
create table B
(
userid int,
info varchar(10)
)
go
insert into B
select 1,'xxx' union all
select 2,'xxx' union all
select 3,'xxx'
go
select * from B where charindex(','+ltrim(userid)+',',(select ','+userids+',' from A where id=1))>0
go
/*
userid info
----------- ----------
1 xxx
2 xxx(2 行受影响)
*/
上面写的好像都不是拼sql语句的.加逗号是为了防止出错,如 5 可以从 15 中找到,但 ,5, 就不能从 ,15, 中找到了.