不明白,要查询A表的所有符合条件的记录以及B表10条符合条件的记录,那么这两个结果集之间有什么关系?没有关系怎么能用一个SQL实现?

解决方案 »

  1.   

    select * from 表a where aArticleChannel = 'C2,C107,T15'select top 10 * from 表b where aKey in (select aKey from 表a where aArticleChannel = 'C2,C107,T15')
    union
    select top 10 * from 表b where aKeyParentIDin (select aKeyParentIDfrom 表a where aArticleChannel = 'C2,C107,T15')
    union
    select top 10 * from 表b where aKeyParentPath (select aKeyParentPath 表a where aArticleChannel = 'C2,C107,T15')以上是我瞎猜的結果,嘿嘿...
      

  2.   

    我再试着说一下,现在有些头疼..有一个文章表A,和一个分类表BA中有字段Channel,内容代表着文章归于哪个分类,而这文章可以属于多个分类,如内容为"C338,C107",即表示此篇文章即属于分类C338,又属于分类C107我现在要列出C107的所有文章,并且还要一起列出C107分类的所有子分类下的文章(包含C107的文章).B中的子分类是这样定义的,它有一个字段aKeyParentPath记录树结构,内容是"C2,C30,C107,C108,C120,C300"那我要列出所有C107的文章和所有C108,C120,C300这些子分类的文章怎么写?这就是我的目的.
      

  3.   

    --这样查询 
    declare @id varchar(10)
    set @id='C107'  --查询C107及其所有子类--查询
    select top 8000 id=identity(int) into #t from syscolumns a,syscolumns bselect a.*
    from 表A a,(
    --要解子类的id列表
    select distinct id=','+substring(a.aKeyParentPath,b.id,charindex(',',a.aKeyParentPath+',',b.id)-b.id)+','
    from 表B a,#t b
    where charindex(','+@id+',',','+a.aKeyParentPath+',')>0
    and len(a.aKeyParentPath)>=b.id
    and substring(','+a.aKeyParentPath,b.id,1)=','
    ) b where charindex(','+@id+',',','+a.Channel+',')>0 --C107的所有文章
    or charindex(b.id,','+a.Channel+',')>0      --C107的所有子类文章
    drop table #t
      

  4.   


    --或者写成存储过程
    create proc p_qry
    @id varchar(10)  --查询的Channel
    as
    set nocount on
    --查询
    select top 8000 id=identity(int) into #t from syscolumns a,syscolumns bselect a.*
    from 表A a,(
    --要解子类的id列表
    select distinct id=','+substring(a.aKeyParentPath,b.id,charindex(',',a.aKeyParentPath+',',b.id)-b.id)+','
    from 表B a,#t b
    where charindex(','+@id+',',','+a.aKeyParentPath+',')>0
    and len(a.aKeyParentPath)>=b.id
    and substring(','+a.aKeyParentPath,b.id,1)=','
    ) b where charindex(','+@id+',',','+a.Channel+',')>0 --C107的所有文章
    or charindex(b.id,','+a.Channel+',')>0           --C107的所有子类文章
    go--调用
    exec p_qry 'C107'
      

  5.   

    調用存儲過程干嗎一定要exec,多余罷
      

  6.   

    还有就是我列出来的要按照表A中的aArticleDate desc方式排序,我没时间研究了,我写的会出现重复值,写在哪里?要用GROUPBY吗?
      

  7.   


    --或者写成存储过程
    create proc p_qry
    @id varchar(10)  --查询的Channel
    as
    set nocount on
    --查询
    select top 8000 id=identity(int) into #t from syscolumns a,syscolumns bselect distinct a.*
    from 表A a,(
    --要解子类的id列表
    select distinct id=','+substring(a.aKeyParentPath,b.id,charindex(',',a.aKeyParentPath+',',b.id)-b.id)+','
    from 表B a,#t b
    where charindex(','+@id+',',','+a.aKeyParentPath+',')>0
    and len(a.aKeyParentPath)>=b.id
    and substring(','+a.aKeyParentPath,b.id,1)=','
    ) b where charindex(','+@id+',',','+a.Channel+',')>0 --C107的所有文章
    or charindex(b.id,','+a.Channel+',')>0           --C107的所有子类文章
    order by a.aArticleDate desc
    go--调用
    exec p_qry 'C107'