按班級分組查詢,每組查詢出5條資料。
資料表結構如下:
drop table if exists `test1`;
create table `test1` (
`id` int(11) not null auto_increment,
`class` int(11) default null comment '班級',
`name` text comment '名姓',
實現sql:
select t.* from test1 t where 5>(select count(*) from test1 where class=t.class and id因為2次select 的都是同一張表,所以需要用 "from test1 t" 區分下。
查詢結果如下圖:
mysql 分組查詢每組的最新一條資料
1.原始資料 學生成績表 2.想要獲取每個考生最新的考試成績,網上的例子 select a.from select from scoreinfo order by scoreinfo.createtime desc as a group by a.snum order by a.createtime...
sql server 分組後,取每組前1條資料
sql 排名開窗函式 row number dense rank rank ntile屬於排名函式。排名開窗函式可以單獨使用order by 語句,也可以和partition by同時使用。partition by用於將結果集進行分組,開窗函式應用於每一組。oder by 指定排名開窗函式的順序。在...
MySQL 查詢分頁資料中分組後取每組的前N條記錄
在使用資料庫查詢的時候,如果遇到對分頁的資料分組,取每組的前n條,實際就是兩次分頁,先分頁,在對分組的每組排序分頁。sql 如下 select a.from select t1.select count 1 from 表 where 分組字段 t1.分組字段 and 排序欄位as group id ...