有表product(产品表)和imgPath(路径表)
product中字段 id,name.
imgPath中字段 id,foreignId(外键id为product中的id),imgPath
imgPath可以存储一个foreignid的多个图片.
比如数据如下
product
id       name
1        特公鸡
2        细米
3        诺鸡鸭
imgPath
id       foreignId    imgPath
1        1            /images/001.jpg
2        1            /images/002.jpg
3        2            /images/003.jpg
现在取值的时候如何只取一张图片?
即:
1   特供机   /images/001.jpg
2   细米     /images/003.jpg

解决方案 »

  1.   

      
      默认取foreignId 为1 的第一条数据 还是?
      

  2.   

    默认获取imgPath中的foreignId的同样编号的第一条
      

  3.   

    select from product p left join imgPath i
    on i.foreignId=p.id and id in (select min(id) from imgPath group by foreignId)
      

  4.   

    select 
           name,
          (select MIN(imgPath.imgPath) from imgPath where imgPath.foreignId=product.id) 
    from product 
      

  5.   

    select  pro.id,pro.name,img.imgPath  from product pro inner join imgPath img on  pro.foreignId=img.id
      

  6.   

    还有一个就是 ,如果imgPath中没有数据!一样的要把product中的数据获取到
      

  7.   

    google:sql server partition by
    http://www.cnblogs.com/sanlang/archive/2009/03/24/1420360.html
    用将图片分组 取第一个(反正就是想办法取图片的一个)
    然后在join一下product(select *,row_number() over(partition by foreignId order by id asc) as Sequence from Img)
    where Sequence=1要sql server 2005以前才可以~
      

  8.   


    select product.*,B.imgPath from product  OUTER Apply (SELECT TOP 1 * FROM  imgPath WHERE imgPath.foreignId=product.ID ORDER BY imgPath.ID) B
      

  9.   


    create table product(id int ,name varchar(10))
    insert into product 
    select 1,'特公鸡' union  
    select 2,'细米' union
    select 3,'诺鸡鸭'  create table imgPath(id int ,foreignid int,imgpath varchar(50))
    insert into imgPath 
    select 1,1, '/images/001.jpg'union  
    select 2,1,'/images/002.jpg'union
    select 3,2, '/images/003.jpg'select a.id,a.name,b.foreignid,b.imgpath from product a left join  imgPath b on 
    a.id=b.foreignid where b.id in(select min(id) from imgPath group by foreignId)--
    id          name       foreignid   imgpath
    ----------- ---------- ----------- --------------------------------------------------
    1           特公鸡        1           /images/001.jpg
    2           细米         2           /images/003.jpg
      

  10.   


    OUTER Apply  在access中应该没有  2005的  
    access 换成left outer应该可以
      

  11.   

    当imgPath中没有product的数据是,获取不到product中的数据呀 ?
      

  12.   


    那就参考 9L的 OUTER Apply (找到匹配值则有值,没有找到匹配值则以NULL表示)
      

  13.   

    select a.id,a.name,b.foreignid,b.imgpath  from product a left join imgPath b
    on a.id=b.foreignid and b.id in(select min(id) from imgPath group by foreignId)
      

  14.   


    b.id in(select min(id) from imgPath group by foreignId)此條件放WHERE后面肯定沒有了
      

  15.   

    用这个试试,我也是前两天才知道的。。select * from product where id in(
    select foreignId from ( select idd=row_number()over(partition by foreignId order by getdate()),* from imgPath)t where idd=1 order by orderNo desc
    )
      

  16.   

    select  
      name,
      (select MIN(imgPath.imgPath) from imgPath where imgPath.foreignId=product.id)  
    from product