PhotoID     ShopID      PhotoName        PhotoDefault AddDate
----------- ----------- ---------------- ------------ --------------------
8           76          4164022546.jpg   0            2007-06-04 16:40:00
9           53          4164215171.jpg   0            2007-06-04 16:42:00
10          53          4164612343.jpg   1            2007-06-04 16:46:00
11          53          4164655671.jpg   0            2007-06-04 16:47:00
12          53          417650828.jpg    0            2007-06-04 17:07:00
13          3           4223634562.jpg   0            2007-06-04 22:37:00
14          76          422395162.jpg    0            2007-06-04 22:40:00ShopID中相同的数值只保留一条,满足保留的条件为:PhotoDefault为最大值,或者AddDate为最大值。

解决方案 »

  1.   

    select * from table a
    where not exists(select 1 from table  where shopid=a.shopid and
     adddate>a.adddate)
      

  2.   

    jinjazz(近身剪) :查询结果和我要的有点出入。我要的结果是如果PhotoDefault为1的话,那么重复记录就显示这条PhotoDefault为1的记录,如果PhotoDefault为0的话那么就取AddDate最大值。
      

  3.   

    你给的语句我测试的结果是这样的:PhotoID     ShopID      PhotoName        PhotoDefault AddDate
    ----------- ----------- ---------------- ------------ --------------------
    12          53          417650828.jpg    0            2007-06-04 17:07:00
    13          3           4223634562.jpg   0            2007-06-04 22:37:00
    14          76          422395162.jpg    0            2007-06-04 22:40:00
      

  4.   

    条件为:PhotoDefault为最大值
    select * from  a b
    where not exists(select 1 from a  where shopid=b.shopid and
     PhotoDefault>b.PhotoDefault)
    order by b.ShopID条件为:AddDate为最大值
    select * from  a b
    where not exists(select 1 from a  where shopid=b.shopid and
     adddate>b.adddate)
    order by b.ShopID
      

  5.   

    我要的结果应该是这样的:PhotoID     ShopID      PhotoName        PhotoDefault AddDate
    ----------- ----------- ---------------- ------------ --------------------
    10          53          4164612343.jpg   1            2007-06-04 16:46:00
    13          3           4223634562.jpg   0            2007-06-04 22:37:00
    14          76          422395162.jpg    0            2007-06-04 22:40:00
      

  6.   

    不能用order by,我这条语句是作为子句用的。