写错,上面的例如, 应该ttitle=5

解决方案 »

  1.   

    哦,如看不到图看这里:
    表 news
    nid  ntitle       nclude
    1    来于于         1,3
    2   来吧,朋友      1
    3    高兴           3,2
    4    同意           2,5
    5    互动式         1,2,3,4,5表title
    tid  ttitle       tname
    1     1            修文来源
    2     1            历史文化
    3     2            文化咨询
    4     2            旅游咨询
    5     3            消费指南
      

  2.   

    SELECT
    A.*
    FROM news A,
    title B
    WHERE B.tid = 1  -- 查询的条件
    AND CHARINDEX(',' + RTRIM(B.tid) + ',', ',' + A.nclude + ',') > 0
      

  3.   

    当ntitle=3时
    结果
    返回
    tid  ttitle      tname 
    4    同意          2,5 
    5    互动式        1,2,3,4,5 
      

  4.   

    ZJCXC 将你的CODe用了一下,发生以下错误,
    将 varchar 值 '修文概况' 转换为数据类型为 int 的列时发生语法错误
      

  5.   

    上面的是多对多呀
    如 ttitle 里有几条都是 1
    如就ttitle=2
    就有两条
    tid  ttitle      tname 
    3    2            文化咨询 
    4    2            旅游咨询 
    再用些去查询news里
    应该返回
    就有
    nid  ntitle      nclude 
    1    来于于        1,3 
    3    高兴          3,2 
    5    互动式        1,2,3,4,5 
      

  6.   

    --try
    SELECT
        A.*
    FROM news A,
        title B
    WHERE B.ttitle = 1  -- 查询的条件
        AND CHARINDEX(',' + RTRIM(B.tid) + ',', ',' + A.nclude + ',') > 0
      

  7.   

    --> --> (Andy)生成测试数据 2008-11-02
    Set Nocount On
    declare @news table([nid] int,[ntitle] nvarchar(5),[nclude] nvarchar(9))
    Insert @news
    select 1,N'来于于',N'1,3' union all
    select 2,N'来吧,朋友',N'1' union all
    select 3,N'高兴',N'3,2' union all
    select 4,N'同意',N'2,5' union all
    select 5,N'互动式',N'1,2,3,4,5'declare @title table([tid] int,[ttitle] int,[tname] nvarchar(4))
    Insert @title
    select 1,1,N'修文来源' union all
    select 2,1,N'历史文化' union all
    select 3,2,N'文化咨询' union all
    select 4,2,N'旅游咨询' union all
    select 5,3,N'消费指南'Declare @ttitle int
    Set @ttitle=2Select * From @news  a
    Where Exists(Select 1 From @title Where ttitle=@ttitle And Charindex(','+Rtrim(tid)+',',','+Rtrim(a.nclude)+',')>0)/*
    nid         ntitle nclude
    ----------- ------ ---------
    1           来于于    1,3
    3           高兴     3,2
    5           互动式    1,2,3,4,5
    */
      

  8.   

    create table news(nid int,ntitle nvarchar(10),nclude varchar(10))
    insert into news select 1,'来于于','1,3'
    insert into news select 2,'来吧,朋友','1'
    insert into news select 3,'高兴','3,2' 
    insert into news select 4,'同意','2,5' 
    insert into news select 5,'互动式','1,2,3,4,5'create table title(tid int,ttitle int, tname nvarchar(10))
    insert into title select 1,1,'修文来源' 
    insert into title select 2,1,'历史文化' 
    insert into title select 3,2,'文化咨询' 
    insert into title select 4,2,'旅游咨询' 
    insert into title select 5,3,'消费指南'select ntitle from news where nclude like '%'+convert(varchar,1)+'%'
    --如果要从title表间接查:
    select ntitle from news where nclude like '%'+convert(varchar,(select top 1 tid from title where tname='修文来源'))+'%'
    go
    drop table news,title
    /*
    ntitle
    ----------
    来于于
    来吧,朋友
    互动式
    */
      

  9.   

    用zjcxc 的,在这里我改一下,我查询条件是ntitle,他上面写的是nid
    所以,我改成这样
    SELECT
        A.*
    FROM news A,
        title B
    WHERE B.ntitle= 2  -- 查询的条件
        AND CHARINDEX(',' + RTRIM(B.nid) + ',', ',' + rtrim(A.nclude) + ',') > 0
    再说,上面他写的nid是我表的主键
    而ntitle不是主键
    用上面的语句后,提示
    将 varchar 值 '修文概况' 转换为数据类型为 int 的列时发生语法错误
      

  10.   

    注:
    Rtrim(a.nclude) 可以不用Rtrim()函数
      

  11.   

    WHERE B.ntitle= 2  -- 查询的条件
    ------------------------------- 你不是查 ntitle 吗? 这个不是字符数据吗? 你用 2 干嘛?
      

  12.   


    select distinct news.*  from 
       (select tid from title where ttitle=2)  a cross join news
    where nclude like '%'+cast(tid  as varchar(2))+'%'结果:1 来于于 1,3
    3 高兴 3,2
    5 互动式 1,2,3,4,5
      

  13.   

    因为我表title
    里,我是用ntitle来查询且里面有2呀
    所有就用
    WHERE B.ntitle= 2来查询
      

  14.   

    ntitle 是字符型, 即使有2, 也是字符, 你不能给数字啊WHERE B.ntitle= '2' 
      

  15.   

    检查你的数据吧, 前面已经有人写过测试了, 方法是对的另外, 你的 nclude 列, 值之间不能有逗号之外的数据, 否则是查不出来的
      

  16.   

    谢谢你们给我这么多的回复,可是问题依然还不能解决,能否直接QQ远程帮我看一下好吗?
    想了好几天了,可是还无法解决
    请帮帮加我一下好吗?QQ:252377274
    也可以告诉你的QQ,我来加一下谢谢