请教一SQL语句,数据如下:A表:
code cname place
01 新闻中心 外网
0001 公司新闻 外网
0002 行业新闻 外网10 新闻中心 内网
1001 公司公告 内网
1002 行业新闻 内网B表:
id title type detail
1 标题1 0001,1001 内容1
2 标题2 0001, 内容2
3 标题3 ,1001 内容3
4 标题5 0001,1001 内容4要求一SQL语句,能到得所有外网的新闻?要麻写?谢谢。
code cname place
01 新闻中心 外网
0001 公司新闻 外网
0002 行业新闻 外网10 新闻中心 内网
1001 公司公告 内网
1002 行业新闻 内网B表:
id title type detail
1 标题1 0001,1001 内容1
2 标题2 0001, 内容2
3 标题3 ,1001 内容3
4 标题5 0001,1001 内容4要求一SQL语句,能到得所有外网的新闻?要麻写?谢谢。
from a,b
where a.code = b.code and a.place = '外网'
select title from b
where exists (select 1 from a where a.place='外网' and charindex(b.code+',',type,1)>0)
select
b.*
from
b inner join
a on charindex(','+ b.[type] +',' ,
',' + a.code + ',') <>02/
select
b.*
from
b inner join
a on (',' + a.code + ',' like ','+ b.[type] +',' )
if object_id('[A表]') is not null drop table [A表]
create table [A表] (code varchar(4),cname varchar(8),place varchar(4))
insert into [A表]
select '01','新闻中心','外网' union all
select '0001','公司新闻','外网' union all
select '0002','行业新闻','外网' union all
select '10','新闻中心','内网' union all
select '1001','公司公告','内网' union all
select '1002','行业新闻','内网'select * from [A表]
--> 测试数据: [B表]
if object_id('[B表]') is not null drop table [B表]
create table [B表] (id int,title varchar(5),type varchar(40),detail varchar(5))
insert into [B表]
select 1,'标题1','0001,1001','内容1' union all
select 2,'标题2','0001','内容2' union all
select 3,'标题3','1001','内容3' union all
select 4,'标题5','0001,1001','内容4'select b.*,a.* from [B表] as b
inner join [A表] as a on charindex(','+a.code+',',','+b.type+',')>0 and a.place='外网'
/*
1 标题1 0001,1001 内容1 0001 公司新闻 外网
2 标题2 0001 内容2 0001 公司新闻 外网
4 标题5 0001,1001 内容4 0001 公司新闻 外网
*/
inner join A表 as a on charindex(','+a.code+',',','+b.type+',')>0 and a.place='外网'
没有用过