文章表:Article
文章图片表:ArticleImage两者为一对多关系。两表根据 ID与AritlceID进行关联。现要求取出5篇文章,同时取出这5篇文章对应的一张图片信息(对图片有宽,高有要求)。
一篇文章会存在多个宽,高相同的图片信息,此时只需取出符合宽,高要求的任意一张图片即可。下面是我的SQL语句,总觉得不太好,不知如何优化。
declare @Article table(ID int identity(1,1), Title nvarchar(200) )
declare @ArticleImage table(ImageID uniqueidentifier,
ArticleID int,Width int,Height int,ImageUrl nvarchar(500))insert into @Article
select '文章1'
union all 
select '文章2'
union all 
select '文章3'
union all 
select '文章4'
union all 
select '文章5'
union all
select '文章6'
union all 
select '文章7'
union all 
select '文章8'
union all 
select '文章9'
union all 
select '文章10'insert into @ArticleImage
select newid(),1,200,300,'图片地址1'
union all 
select newid(),1,200,300,'图片地址2'
union all 
select newid(),2,200,300,'图片地址3'
union all 
select newid(),2,200,300,'图片地址4'
union all 
select newid(),3,200,300,'图片地址5'
union all 
select newid(),3,200,300,'图片地址6'
union all 
select newid(),4,200,300,'图片地址7'
union all 
select newid(),4,200,300,'图片地址8'
union all 
select newid(),5,200,300,'图片地址9'
union all 
select newid(),5,200,300,'图片地址10'
select top 5 ID,Title,
(select top 1 ImageUrl from @ArticleImage
where ArticleID=ID and Width=200 and Height=300) as ArticleImageUrl,
(select top 1 ImageID from @ArticleImage
where ArticleID=ID and Width=200 and Height=300) as ArticleImageID
from @Article

解决方案 »

  1.   

    //查询结果
    1 文章1 图片地址1 B167B6B6-09FB-48A7-8EAF-F757D59FF3CC
    2 文章2 图片地址3 A9E989FE-65D4-4E6B-A9D7-56BDA7DBE56D
    3 文章3 图片地址5 72CC1F3A-4874-4D73-923F-50C5DB93EF4C
    4 文章4 图片地址7 EB02A87E-9C0C-4BCE-812C-8961251D77F5
    5 文章5 图片地址9 9D714DF0-949C-4ECC-BAFB-4876A2381C7D
      

  2.   


    SELECT  TOP 5
    A.ID
    ,A.Title
    ,A.ImageUrl
    ,A.ImageID
    FROM    @Article AS A
    INNER JOIN  @ArticleImage AS I
    ON  I.ArticleID = A.ID
    AND I.Width = 200 
    AND  I.Height = 300
    ORDER BY A.ID   
      

  3.   


    1对多的,这里的要求,用inner不行的。
    你的查询结果是:
    1 文章1 图片地址1 321532B5-DC1F-4FB0-8769-8069C0861276
    1 文章1 图片地址2 B710C2C1-D821-4C5C-AF30-DA6DCB8C9247
    2 文章2 图片地址3 35B8C041-F3C6-433E-96AC-B12AD6FC42A7
    2 文章2 图片地址4 6722CD99-6A59-45F8-9DE7-D15173BF881A
    3 文章3 图片地址5 E60767D5-EF80-4A88-B133-4E0B56CE935E
      

  4.   

    select 
       *
    from
       @Article  a,@ArticleImage b
    where
       a.id=b.ArticleID
    and
       ImageUrl=(select min(ImageUrl) from @ArticleImage where ArticleID=b.ArticleID)
      

  5.   

    declare @Article table(ID int identity(1,1), Title nvarchar(200) ) 
    declare @ArticleImage table(ImageID uniqueidentifier, ArticleID int,Width int,Height int,ImageUrl nvarchar(500))   
    insert into @Article 
    select '文章1'
    union all 
    select '文章2'
    union all 
    select '文章3'
    union all 
    select '文章4'
    union all 
    select '文章5'
    union all 
    select '文章6'
    union all 
    select '文章7'
    union all 
    select '文章8'
    union all 
    select '文章9'
    union all 
    select '文章10'  
    insert into @ArticleImage 
    select newid(),1,200,300,'图片地址1'
    union all 
    select newid(),1,200,300,'图片地址2'
    union all 
    select newid(),2,200,300,'图片地址3'
    union all 
    select newid(),2,200,300,'图片地址4'
    union all 
    select newid(),3,200,300,'图片地址5'
    union all 
    select newid(),3,200,300,'图片地址6'
    union all 
    select newid(),4,200,300,'图片地址7'
    union all 
    select newid(),4,200,300,'图片地址8'
    union all 
    select newid(),5,200,300,'图片地址9'
    union all 
    select newid(),5,200,300,'图片地址10'
    select  
    a.* ,b.ArticleID,b.ImageUrl
    from   
    @Article  a,@ArticleImage b 
    where   
    a.id=b.ArticleID 
    and   
    ImageUrl=(select min(ImageUrl) from @ArticleImage where ArticleID=b.ArticleID)

    /*ID          Title                                                                                                                                                                                                    ArticleID   ImageUrl
    ----------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    1           文章1                                                                                                                                                                                                      1           图片地址1
    2           文章2                                                                                                                                                                                                      2           图片地址3
    3           文章3                                                                                                                                                                                                      3           图片地址5
    4           文章4                                                                                                                                                                                                      4           图片地址7
    5           文章5                                                                                                                                                                                                      5           图片地址10(5 行受影响)*/
      

  6.   


    F大, 这样就不能对width,height进行限制了啊。
    比如要规定 width=200 and height=400   这样只能跑出一条记录。select  
        a.* ,b.ArticleID,b.ImageUrl
    from   
        @Article  a,@ArticleImage b 
    where   
        a.id=b.ArticleID 
    and
    b.width=200
    and
    b.height=400
    and   
        ImageUrl=(select min(ImageUrl) from @ArticleImage where ArticleID=b.ArticleID)
      

  7.   

    一篇文章会有多个不同宽,高尺寸的图片。
    用这个测试数据吧。insert into @ArticleImage 
    select newid(),1,200,300,'图片地址1'
    union all 
    select newid(),1,200,300,'图片地址2'
    union all 
    select newid(),2,200,300,'图片地址3'
    union all 
    select newid(),2,200,300,'图片地址4'
    union all 
    select newid(),3,200,300,'图片地址5'
    union all 
    select newid(),3,200,300,'图片地址6'
    union all 
    select newid(),4,200,300,'图片地址7'
    union all 
    select newid(),4,200,300,'图片地址8'
    union all 
    select newid(),5,200,300,'图片地址9'
    union all 
    select newid(),5,200,300,'图片地址10'
    UNION ALL
    select newid(),1,200,400,'图片地址11'
    union all 
    select newid(),1,200,400,'图片地址12'
    union all 
    select newid(),2,200,400,'图片地址13'
    union all 
    select newid(),2,200,400,'图片地址14'
    union all 
    select newid(),3,200,400,'图片地址15'
    union all 
    select newid(),3,200,400,'图片地址16'
    union all 
    select newid(),4,200,400,'图片地址17'
    union all 
    select newid(),4,200,400,'图片地址18'
    union all 
    select newid(),5,200,400,'图片地址19'
    union all 
    select newid(),5,200,400,'图片地址20'
      

  8.   


    select top 5 ID,Title,T.ImageID,T.ImageUrl From @Article
    cross apply (select top 1 * from @ArticleImage where ArticleID=ID and Width=200 and Height=300) as T