MySQL按分类查询指定数量的数据

鸿辰 MySQL 2.67 K
  • 说明

    此SQL仅供参考,在数据量大时,需要考虑优化!

  • SQL语句

    SELECT 
      a.*
    FROM
      edu_course AS a
    WHERE
      (SELECT 
              COUNT(*)
          FROM
              edu_course AS b
          WHERE
              b.course_type = a.course_type AND b.id >= a.id) <= 2
    ORDER BY a.course_type ASC , a.id DESC
    • course_type 为分类字段

标签: MySQL