MySql按字段分组取最大值记录
数据库原始数据如下:数据库名:tbl_clothers

需求是:按照type分组,并获取个分组中price中的最大值,解决sql如下:
方法一:
select *
from (select type, name, price from tbl_clothers order by price desc) as a
方法二:
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 = ?
建立索引ALERT TABLE ADD INDEX update_fid (fid, updatetime)
有些语法可能会由于数据裤版本不同,会有差别。
MySql按字段分组取最大值记录的更多相关文章
- MySql按字段分组取最大值记录 [此博文包含图片]
要求:获得按table1_id分组,并且age最大的记录信息,即2.3.5条 方法一: select * from (select * from table2 order by age d ...
- MySQL更新指定分组中最大值记录
数据表中存储着某个状态字段,当分组中所有数据入库后,需要根据相应的最大值更新状态字段,如表: 现在要将no=11的分组中把最大值记录的status更新为1,可以直接这么写: ; done~
- mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据 mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结 ...
- SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...
- [sql server、oracle] 分组取最大值最小值常用sql
sqlserver2005前: --分组取最大最小常用sql--测试环境if OBJECT_ID('tb') is not null drop table tb;gocreate table tb( ...
- javascript 从对象数组中 按字段/属性取最大值或最小值
var array=[ { "index_id": 119, "area_id": "18335623", "name" ...
- T-SQL百万记录中分组取最大值方法ROW_NUMBER() OVER()
SELECT SysUserID, UserID, ROW_NUMBER() OVER(PARTITION BY UserID ORDER BY AddTime DESC) AS nums AND S ...
- MYSQL根据分类分组取每组一条数据且按条件能排序的写法
之前在一个项目的开发中,有遇到要根据分类来分组获取每组一条按某个条件字段排序的数据结果,于是先自己写了一条语句: select * from `表A` GROUP BY `c`; 上面这个语句有可以根 ...
- oracle 根据字段分组取第一条数据及rank函数说明
当前有这样一个需求,根据外键对子表数据进行分组,取每组中的一条数据就行了,如图: 如:COMMANDID = 26的有两条,只取一条数据. sql语句: select * from(select SY ...
随机推荐
- 用vs2013开发node.js的addon.
下载node.js的源代码. https://github.com/joyent/node 如果用svn下载,后面加上/trunk,以免把用不着的branches也下载下来,浪费时间. 安装V ...
- [Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)
This post is similar to previous post. The difference is in this post, we are going to see how to ha ...
- [Angular-Scaled web] 2. Architecture sub-modules
Common models will be a sub models for category and bookmarks. Because they are used everywhere. For ...
- Jmeter-Maven-Plugin高级应用:Remote Server Configuration
Remote Server Configuration Pages 12 Home Adding additional libraries to the classpath Advanced Conf ...
- (回溯法)和为n的所有不增正整数和式分解算法
题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...
- Java从零开始学三(public class和class)
使用public class和class声明的区别 public class文件名称必须与类名称一致 class文件名称可以与类名称不一致
- Linux内核结构体--kfifo 环状缓冲区
转载链接:http://blog.csdn.net/yusiguyuan/article/details/41985907 1.前言 最近项目中用到一个环形缓冲区(ring buffer),代码是由L ...
- margin和padding的学习
你在学习margin和padding的时候是不是懵了--什么他娘的内边距,什么他娘的外边距.呵呵呵,刚開始我也有点不理解,后来通过查资料学习总算弄明确了,如今我来谈一下自己对margin和paddin ...
- Oracle 跨库查询表数据(不同的数据库间建立连接)
1.情景展示 当需要从A库去访问B库中的数据时,就需要将这两个库连接起来: 两个数据库如何实现互联互通,在oracle中,可以通过建立DBLINK实现. 2.解决方案 2018/12/05 第一步 ...
- JAVA遍历Map的方法
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TestMap { pu ...