如果是取ID最大的,SELECT 
h.id,
h.username,
h.qname,
h.qsex,
h.nian,
h.sg,
h.hy_sf,
p.photo
from hy h ,
(Select * from hy_photo Where Id In (Select Max(id) from hy_photo)) p 
where h.username=p.username

解决方案 »

  1.   

    SELECT 
        h.username username ,
        h.id id ,
        h.qname qname,
        h.qsex qsex,
        h.nian nian,
        h.sg sg,
        h.hy_sf hy_sf, 
        p.photo photo,
        p.dj dj  
    from 
       hy h ,
       hy_photo p 
    where 
       h.username=p.username 
       and
       p.id = (select top 1 id from hy_photo where username=p.username order by dj desc)
      

  2.   

    如果是取每个hy_id 点击数最大的
    SELECT 
    h.id,
    h.username,
    h.qname,
    h.qsex,
    h.nian,
    h.sg,
    h.hy_sf,
    p.photo
    from hy h ,
    (Select A.* from hy_photo A Inner Join (Select hy_id,Max(dj) from hy_photo Group By hy_id) B On A.hy_id=B.hy_id) p 
    where h.username=p.username
      

  3.   

    小铃子专行了.
    select A.id,A.username,A.qname,A.qsex,A.nian,A.sg,A.hy_sf,B.photo,max(B.dj) from hy A INNER JOIN hy_photo B on A.username=B.username group by A.id,A.username,A.qname,A.qsex,A.nian,A.sg,A.hy_sf,B.photo
      

  4.   

    SELECT h.username username ,h.id id ,
    h.qname qname,h.qsex qsex,h.nian nian,
    h.sg sg,h.hy_sf hy_sf, p.photo photo  
    from hy h ,hy_photo p 
    where h.username=p.username
    and not exists(
    select * from hy_photo
    where username=p.username
    and dj>p.dj)
      

  5.   

    SELECT h.username username ,h.id id ,
    h.qname qname,h.qsex qsex,h.nian nian,
    h.sg sg,h.hy_sf hy_sf, p.photo photo  
    from hy h ,hy_photo p 
    where h.username=p.username
    and not exists(
    select * from hy_photo
    where username=p.username
    and (dj>p.dj or dj=p.dj and id<p.id))
      

  6.   

    谢谢 zjcxc(邹建) 大哥的帮助,和其他各位的帮助.
    谢谢大家了.只是, zjcxc(邹建) 大哥,能不能问一下:
    where h.username=p.username
    and not exists(
    select * from hy_photo
    where username=p.username
    and (dj>p.dj or dj=p.dj and id<p.id))
    中的not exists()是什么意思呢.
    :)
      

  7.   

    邹老大:你好
    我的帖子已经结了.
    可是又有新问题了.
    如果,hy里面有d会员,但是,d会员没有照片,我还是要显示d会员怎么办呢
    SQLrec="SELECT h.username username ,h.id id ,h.qname qname,h.qsex qsex,h.nian nian, h.sg sg,h.hy_sf hy_sf, p.photo photo ,p.dj dj  from hy h ,hy_photo p where h.username(+)=p.username and not exists( select * from hy_photo where username=p.username and (dj>p.dj or dj=p.dj and id<p.id))"
    我用外链接怎么会不对呢.能不能再帮我一次.
      

  8.   

    用左连接如果是取每个hy_id 点击数最大的
    SELECT 
    h.id,
    h.username,
    h.qname,
    h.qsex,
    h.nian,
    h.sg,
    h.hy_sf,
    p.photo
    from hy h 
    Left Join
    (Select A.* from hy_photo A Inner Join (Select hy_id,Max(dj) from hy_photo Group By hy_id) B On A.hy_id=B.hy_id) p 
    On h.username=p.username
    邹老大的方法并不是很好的
      

  9.   

    SQLrec="SELECT h.username username ,h.id id ,h.qname qname,h.qsex qsex,h.nian nian, h.sg sg,h.hy_sf hy_sf, p.photo photo ,p.dj dj  from hy h  left join hy_photo p on h.username=p.username where h.username like '%"&username&"%' and h.qsex like '%"&qsex&"%' and h.nian like '%"&nian&"%' and h.hy_sf like '%"&hy_sf&"%' and  not exists(select * from hy_photo where username=p.username and (dj>p.dj or dj=p.dj and id<p.id))"
      

  10.   

    这里用Exists来判断,效率不一定高。
      

  11.   

    SELECT h.username username ,h.id id ,h.qname qname,h.qsex qsex,h.nian nian,       h.sg sg,h.hy_sf hy_sf, p.photo photo ,p.dj dj  from hy h 
    left join hy_photo p on h.username(+)=p.username and not exists(select * from hy_photo where username=p.username and (dj>p.dj or dj=p.dj and id<p.id))