Mysql查询比较

创建一个表:

article,书编码;dealer,书店;price ,书的价格。

导入一些数据:

INSERT INTO shop VALUES
    (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),
    (3,'C',1.69),(3,'D',1.25),(4,'D',19.95);
需求1:
  获取价格最高的书的相关信息
  sql1:select * from shop where price in (select Max(price) from shop);
  sql2:select * from shop  order by price desc limit 1;
  sql3:select t1.* from shop t1 left join shop t2 on t1.price <t2.price where t2.price is null;
需求2:
  找取每种价格最贵的书的相关信息
  sql1:select * from  shop where price in (select Max(price) from shop group by article);
  sql2:select s1.* from shop s1 inner join (select article,MAX(price) as price from shop group by article )s2 on s1.article=s2.article and s1.price=s2.price;
  sql3:select s1.* from shop s1 where price=(select MAX(price) from shop s2 where s1.article=s2.article);
  sql4:
select s1.* from shop s1 left join shop s2 on s1.article=s2.article and s1.price<s2.price where s2.price is null;(优)
 

  
原文地址:https://www.cnblogs.com/itdev/p/5777372.html