请教一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语句,能到得所有外网的新闻?要麻写?谢谢。

解决方案 »

  1.   

    B表的type和A表的code是关联的么?是的话就好说了。select b.id, b.title, a.cname, a.place, b.detail
    from a,b
    where a.code = b.code and a.place = '外网'
      

  2.   

    没有关联列么?哦,原来type和code对应。
    select title from b
    where exists (select 1 from a where a.place='外网' and charindex(b.code+',',type,1)>0)
      

  3.   

    1/
    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] +',' )
      

  4.   

    --> 测试数据: [A表]
    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 公司新闻 外网
    */
      

  5.   

    select b.*,a.* from B表 as b
    inner join A表 as a on charindex(','+a.code+',',','+b.type+',')>0 and a.place='外网'
      

  6.   

    原来四楼还是把code和type关联了。我还以为没关联可以查
      

  7.   

    charindex 啥玩意
    没有用过