欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

orcale 多行多列合并为多行一列

程序员文章站 2022-03-31 21:44:58
...

前言:

小编最近有一个需求就是一个字段里面存多张表的id,然后界面需要联查显示客户需要的对应的内容。经过一系列的百度搜索实践,最终发现了下面这个可以实现我想要的结果。

内容:

查询出来的结果:

orcale 多行多列合并为多行一列

 

 

第一种:

使用CONCAT(ch1, ch2) 函数,但是这个只适用于有两个参数的。而我的是多张表,不能使用。

SELECT CONCAT(ch1, ch2) FROM t

第二种:

使用“||”  或链接,这样就可以将查询出来的多个字段链接到一块。例如:

sql语句:

SELECT
	buc.COMPANY_NAME,
-- 	gp.*,
	gw.LICENSE_PLATE_NUMBER ,	
	s.id|| b.id ||c.id ||r.id||p.id
		
FROM
	ACC_BUSINESS_USABLE_CHANGE buc
	LEFT JOIN GP_DETAILS gp ON buc.RELEVANCE_BILLS_ID = gp.id  
	LEFT JOIN GP_WBILL gw ON  buc.RELEVANCE_BILLS_ID =  gw.id
	LEFT JOIN SM_SETTLE s  ON buc.RELEVANCE_BILLS_ID=s.id
	LEFT JOIN SM_SETTLE b  ON buc.RELEVANCE_BILLS_ID=b.id
	LEFT JOIN SM_TRANSPORT_CAR c  ON buc.RELEVANCE_BILLS_ID=c.id
	LEFT JOIN SM_PORT_RAILWAY r ON buc.RELEVANCE_BILLS_ID=r.id
	LEFT JOIN SM_SPORT_PORT p ON buc.RELEVANCE_BILLS_ID=p.id
WHERE buc.RELEVANCE_BILLS_ID ='4062201901250001006'
or buc.RELEVANCE_BILLS_ID='4486201904090000007'
or buc.RELEVANCE_BILLS_ID ='4065201811190000004'
or buc.RELEVANCE_BILLS_ID ='4487201811270001001'
or buc.RELEVANCE_BILLS_ID ='4486201902260000061'
or buc.RELEVANCE_BILLS_ID ='4495201901090002'
or buc.RELEVANCE_BILLS_ID ='4496201905213003'
or buc.RELEVANCE_BILLS_ID ='4496201812270001'

orcale 多行多列合并为多行一列

这样的效果还是不能达到我们想要的效果,然后我们使用as 将标红的字段别名更改为我们需要的。

SELECT
	buc.COMPANY_NAME,
-- 	gp.*,
	gw.LICENSE_PLATE_NUMBER ,	
		(s.id|| b.id ||c.id ||r.id||p.id) as pid
		
FROM
	ACC_BUSINESS_USABLE_CHANGE buc
	LEFT JOIN GP_DETAILS gp ON buc.RELEVANCE_BILLS_ID = gp.id  
	LEFT JOIN GP_WBILL gw ON  buc.RELEVANCE_BILLS_ID =  gw.id
	LEFT JOIN SM_SETTLE s  ON buc.RELEVANCE_BILLS_ID=s.id
	LEFT JOIN SM_SETTLE b  ON buc.RELEVANCE_BILLS_ID=b.id
	LEFT JOIN SM_TRANSPORT_CAR c  ON buc.RELEVANCE_BILLS_ID=c.id
	LEFT JOIN SM_PORT_RAILWAY r ON buc.RELEVANCE_BILLS_ID=r.id
	LEFT JOIN SM_SPORT_PORT p ON buc.RELEVANCE_BILLS_ID=p.id
WHERE buc.RELEVANCE_BILLS_ID ='4062201901250001006'
or buc.RELEVANCE_BILLS_ID='4486201904090000007'
or buc.RELEVANCE_BILLS_ID ='4065201811190000004'
or buc.RELEVANCE_BILLS_ID ='4487201811270001001'
or buc.RELEVANCE_BILLS_ID ='4486201902260000061'
or buc.RELEVANCE_BILLS_ID ='4495201901090002'
or buc.RELEVANCE_BILLS_ID ='4496201905213003'
or buc.RELEVANCE_BILLS_ID ='4496201812270001'

效果如下:

orcale 多行多列合并为多行一列