求助,帮忙写个sql语句
A表
ID name others
101 语文 ....
103 数学 ...
201 英语 ....
...
B表
name ID_A
张三 101,103
李四 103,201
现在要写一个查询,
B表中某一记录查出对应的A表其它记录
select * from A where ID in (select ID_A from B where name=张三)
我上面的语句会报错,求高手帮忙解决,谢谢了
[解决办法]
tbaID classid name1 1,2,3 西服 2 2,3 中山装3 1,3 名裤tbb id classname1 衣服2 上衣3 裤子我得的结果是id classname name1 衣服,上衣,裤子 西服 2 上衣,裤子 中山装3 衣服,裤子 名裤create table tba(ID int,classid varchar(20),name varchar(10))insert into tba values(1,'1,2,3','西服')insert into tba values(2,'2,3' ,'中山装')insert into tba values(3,'1,3' ,'名裤')create table tbb(ID varchar(10), classname varchar(10))insert into tbb values('1','衣服')insert into tbb values('2','上衣')insert into tbb values('3','裤子')go--第1种方法,创建函数来显示create function f_hb(@id varchar(10))returns varchar(1000)asbegin declare @str varchar(1000) set from #tinsert into #t select * from #t2drop table #t2endgoselect id_no,convert(char,in_date,120) as in_date from #t a;drop table #t,a;
[解决办法]
ID name others
101 语文 ....
103 数学 ...
201 英语 ....
...
B表
name ID_A
张三 101,103
李四 103,201
select A.* from A,B where charindex(a.id,b.ID_A)>0
[解决办法]
select t1.* from A t1 ,B t2where charindex(','+ID+',',','t2.ID_A+',')>0 and t2.name=张三
[解决办法]
declare @a table (id varchar(10),name varchar(10))insert into @a select '101','语文'insert into @a select '103','数学'insert into @a select '201','英语'declare @b table (name varchar(10),id_a varchar(50))insert into @b select '张三','101,103'insert into @b select '李四','103,201'select * from @a a,@b bwhere charindex(a.id,b.id_a)>0and b.name='张三'