MySQL 根据某个字段排序后,获取一条或者某几条数据

时间:2024-04-13 13:40:04

数据库格式:

MySQL 根据某个字段排序后,获取一条或者某几条数据

需求:根据area分组并统计分组后的总数,根据统计出来的总数降序排序,获取前10条数据。

  <select id="selectByAreaDevNumList" resultMap="BaseResultMap" parameterType="java.util.List" >
    select
    area ,COUNT(*)
    from car_area
    where dev_num IN
    <foreach collection="list" item="list" open="(" separator="," close=")">
      #{list}
    </foreach>
    GROUP  BY area
    order by COUNT(*)
    DESC LIMIT 10
  </select>

mysql中的代码获取如下:

SELECT area ,COUNT(*)
FROM car_area 
GROUP  BY area 
order by COUNT(*)
DESC LIMIT 0,10

 或者可以这么写

SELECT area ,COUNT(*)
FROM car_area 
GROUP  BY area 
order by COUNT(*)
DESC LIMIT 10

运行结果如下:

MySQL 根据某个字段排序后,获取一条或者某几条数据