数据库原始数据如下:数据库名:tbl_clothers
需求是:按照type分组,并获取个分组中price中的最大值,解决sql如下:
方法一:
select * from (select type, name, price from tbl_clothers order by price desc) as a group by a.type;
讯享网
方法二:
讯享网select a.* from tbl_clothers as a where price = (select max(price) from tbl_clothers where a.type=type)
方法三:
select a.* from tbl_clothers as a where not exists (select * from tbl_clothers where type=a.type and price>a.price) # not exists意思是:在tbl_clothers中找不到比a的价格更大的值,也就是a的值应该是最大的价格。
方法四:

讯享网select a.* from tbl_clothers as a where exists (select count(*) from tbl_clothers where type=a.type and price>a.price having count(*)=0)
方法五:
select a.* from tbl_clothers a inner join ( select type,max(price) maxprice from tbl_clothers group by type) b on a.type=b.type and a.price=b.maxprice #order by a.type;
方法六:这个是比较直观的
讯享网SELECT MAX(update_time) AS update_time, fid FROM new GROUP BY fid ORDER BY update_time DESC SELECT * FROM new INNER JOIN user ON user.id = new.user_id WHERE fid = ? AND update_time = ?
简单方法:select * from table order by weight desc limit 1;
有些语法可能会由于数据库版本不同,会有差别。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/93437.html