--表和数据如下
create table class
(cid int,
stu_id varchar(10)
)
insert into class
select 1,'1,2,3' union
select 2,'2,11'
create table student_new
(stu_id int,
stu_name varchar(20)
)
insert into student_new
select 1,'张三丰' union
select 2,'张无忌' union
select 11,'王重阳'
--想要的效果:
cid stu_name
1 '张三丰'
1 '张无忌'
2 '张无忌'
2 '王重阳'
------解决思路----------------------
没有深入研究,不知可行不
select * from class a join student_new b
on CHARINDEX (convert (varchar(10), b.stu_id),a.stu_id)>0
where a.cid<=b.stu_id
------解决思路----------------------
SELECT a.cid,b.stu_name FROM class a JOIN student_new b ON ','+a.stu_id+',' like '%,'+CAST(b.stu_id AS varchar(10) )+',%'
DROP TABLE class
DROP TABLE student_new;
/*
cid stu_name
----------- --------------------
1 张三丰
1 张无忌
2 张无忌
2 王重阳
*/
------解决思路----------------------
http://bbs.csdn.net/topics/390904867
一样的问题 用CHARINDEX 或者like'%'+colname+'%'都可以