表一 User
id,name
1  张三
2  李四表二 Photo
id,imgurl,UserID
1  1.gif  1
2  2.gif  1
3  3.gif  1
4  4.gif  2
5  5.gif  2
现在只想每人显示一张照片
需要 select 出来的结果如下id,name,imgurl
1  张三  1.gif
2  李四  4.gif请问SQL语句要怎么写?数据库为Access

解决方案 »

  1.   

    select * from [User] Where id Not In (Select Min(UserID) As id from Photo Group By imgurl,UserID)
      

  2.   

    select a.id,a.name,t.imgur1 from [user]a
    left join (select top 1 id,imgur1,userid from photo)t on a.id=t.id
      

  3.   

    select a.id,a.name b.imgurl from User as a ,Photo as b where a.id = b.UserID and b.id in (selct top 1 * from Photo  as C where c.UserID =a.id)
      

  4.   

    select a.id,a.name,b.img from 
    (select id,name from user ) a inner join 
    (select min(id)  ,min(imgurl) img ,userid from photo group by userid) b on a.id=b.userid
      

  5.   

    select c.* , d.imgurl from 表一 c , 
    (
      select * from 表二 a,
      (select userid,min(id) as id from 表二 group by userid) b
      where a.id = b.id and a.userid = b.userid
    ) d
    where c.id = d.userid
      

  6.   

    select a.id,a.name,c.imgurl from [User] a 
    inner join (select min(id) as id,UserID from Photo) b 
    on
    a.[id]=b.userid
    inner join
    Photo c
    on
    b.id=c.id
      

  7.   

    declare  @u table(id int,name varchar(8))
    insert @u select
    1,  '张三'
    union all select
    2,  '李四'declare @p table(id int,imgurl varchar(30),UserID int)
    insert @p select
    1,  '1.gif' , 1
    union all select
    2,  '2.gif' , 1
    union all select
    3,  '3.gif'  ,1
    union all select
    4 , '4.gif' , 2
    union all select
    5 , '5.gif' , 2
    select UserID, name, imgurl from @p p 
    join @u u on u.id= p.UserID
    where p.id =(select top 1 id from @p where UserID=p.UserID)UserID      name     imgurl                         
    ----------- -------- ------------------------------ 
    1           张三       1.gif
    2           李四       4.gif(所影响的行数为 2 行)
      

  8.   

    select user.id,user.name,c.imgurl from user ,(select * from photo group by photo.userid) as c where user.id=c.userid;