mysql中group_concat和oracle中LISTAGG


分別在mysql和oracle創建下面兩個表  建表語句來源借鑒

create table plan (
id int PRIMARY key,
name varchar(255)
);


-- 創建一個計划明細表,是計划表的子表
create table planDetail(
id int PRIMARY key,
name varchar(255),
state int,
planId int

);
-- 插入數據
insert into plan values(1,'計划一');
insert into plan values(2,'計划二');
insert into plan values(3,'計划三');

insert into planDetail values(1,'明細一',0,1);
insert into planDetail values(2,'明細2',1,1);
insert into planDetail values(3,'明細3',1,1);

insert into planDetail values(4,'明細一',0,2);
insert into planDetail values(5,'明細2',0,2);
insert into planDetail values(6,'明細3',1,2);

insert into planDetail values(7,'明細一',1,3);
insert into planDetail values(8,'明細2',1,3);
insert into planDetail values(9,'明細3',1,3);

排序用法------------------------------------------------------

-- mysql用法

select t1.name ,group_concat(t2.name order by PLANID asc) from plan t1
LEFT JOIN planDetail t2 on T1.id = T2.planid
GROUP BY T1.name

-- oracle達到相同效果的用法

 

select t1.name ,LISTAGG(t1.name,',') within GROUP (order by t2.planid asc) from plan t1
LEFT JOIN planDetail t2 on T1.id = T2.planid GROUP BY T1.name

效果如下;

計划一  明細3,明細2,明細一
計划三  明細3,明細2,明細一
計划二  明細3,明細2,明細一

--------------------------------------------------------

不排序用法

--- mysql

select t1.name ,group_concat(t2.name) from plan t1 LEFT JOIN planDetail t2 on T1.id = T2.planid GROUP BY T1.name

效果如下:

計划一 明細一,明細2,明細3
計划三 明細一,明細2,明細3
計划二 明細2,明細3,明細一

----oracle

select t1.name ,wm_concat(t2.name) from plan t1 LEFT JOIN planDetail t2 on T1.id = T2.planid GROUP BY T1.name

 

效果如下:

計划二 明細一,明細3,明細2
計划三 明細一,明細3,明細2
計划一 明細一,明細3,明細2


免責聲明!

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



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