select a.*,b.name from t_price a inner join t_product b on 
b.id = a.p_id
where not exists(select 1 from t_price 
where a.p_id=p_id and a.price>price or 
(a.price=price and a.quantity<quantity)
)

解决方案 »

  1.   

    select a.* ,b.name from t_price a inner join t_product b on  
    b.id = a.p_id
    where not exists(select 1 from t_price  
    where a.p_id=p_id and (a.price>price or  
    (a.price=price and a.quantity<quantity)
    ))
      

  2.   

    参考下贴中的多种方法http://topic.csdn.net/u/20091231/16/2f268740-391e-40f2-a15e-f243b2c925ab.html
    [征集]分组取最大N条记录方法征集,及散分....
      

  3.   

    select p_id,retailer_id
    from t_price a 
    where not exists (select 1 from t_price where p_id=a.p_id and (price<a.price or (price=a.price and quantity>a.quantity)))
      

  4.   

    select name,retailer_id from (
    select a.name,b.p_id,b.retailer_id,price,quantity
    from t_product a inner join t_price b on a.id=b.p_id
    order by b.p_id,price,quantity desc
    ) x
    group by name
      

  5.   

    这些方法都是用的subselect, 效率上讲会不会慢啊?假如有上万产品的话
      

  6.   

    上万产品话,添加合适的索引,性能应该不会有太在影响 。select p_id,retailer_id
    from t_price a 
    where not exists (select 1 from t_price where p_id=a.p_id and (price<a.price or (price=a.price and quantity>a.quantity)))