SQL文件
create table shop
(
	article int(10) primary key auto_increment,
	author varchar(10),
	price float(10)
);
 insert into shop
	 (author,price)
	 values
	 ('B',3.99),('A',10.99),('C',1.69),('B',19.95),('A',6.96);
  问题
<mark>选出每个author的最高price,要求显示每个字段?</mark>
 不就是group by 然后根据author分组进行排序吗
 select article,author,max(price) from shop group by author;
  执行结果:
 
 发现一个问题,这边的
 
 article对应不上,主要是因为所有的列都是以author排序后再进行填充的,B的第一次出现位置在1,所以就直接填入1了。
解决方案
使用相关查询
select 
	article ,author,price 
from 
	shop s1 
where 
	price = (
select 
	max(s2.price) 
from 
	shop s2 
where 
	s1.author = s2.author
);
  创建虚拟表进行结合起来查询,查询结果:
 

京公网安备 11010502036488号