group by对应的select子句,只能有分组列和集函数
即在select语句中除了集函数,不能输出非group by的列
如果想要获取非分组列怎么做呢?
将你需要的字段放进max或min函数中,max:支持字符类型、数字类型。
select max(id) as id,username,password from users
group by username,password
像上面的mysql语句,id本不能输出,但是外面套一个max或min函数就可以输出了。
但这样的前提是该列在每个主码上只对应一个值,要不然会少很多
通用的方法:
select a.name,a.desp,b.price
from show_plan as a,(
select desp,MAX(price) as price
from show_plan
group by desp
) as b
where a.price = b.price AND a.desp = b.desp;