SQL中獲取每組中的前兩條 | 每組中第2條 | 每組中指定位置的信息


前提條件:

  使用偽劣實現,對現有的信息進行分組添加序號。

create table if not EXISTS bigdata.test (id int ,name varchar(255), num int);
insert into test VALUES(1,'lili-1', 1);
insert into test VALUES(1,'lili-2', 2);
insert into test VALUES(1,'lili-3', 3);
insert into test VALUES(2,'lu-1', 1);
insert into test VALUES(2,'lu-2', 2);
insert into test VALUES(2,'lu-3', 3);

select * from test where num in (1,2) order by id,num;

-- 過濾num列中想要的信息即可。
-- 如果只想獲取每組的第一行,可以直接分組實現
-- 如果只想獲取每組中前兩行,where后過濾1,2即可。
-- 如果想獲取指定列信息,直接在where后添加即可

表信息:  

結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM