sql 语句优化,将2行查询结果合并到一行。

sql 语句优化,将2行查询结果合并到一行。

问题描述:

select emp_chg_other.s_name,project_table.project_name from kc_billhead,project_table,emp_chg_other,hr_epm_station,fg_ogm_station where kc_billhead.tr_proj=project_table. pc and project_table.pc=emp_chg_other.chg_data and emp_chg_other.emp_id=hr_epm_station.ccode and hr_epm_station.station=fg_ogm_station.cno and hr_epm_station.station='0106' and kc_billhead.billno='CGRK20180102001';

图片说明

有一种死办法,concat(字段1,字段2)
具体写法 select concat((select 字段A from 表 where 条件=第一行),(select 字段A from 表 where 条件=第二行));这样即可

oracle数据库吗?

可以看看这篇文章,sql的纵横查询,希望能够帮助你
http://blog.csdn.net/mango_love/article/details/51210020

这个是mysql数据库把

跪求大神教育,优化,msql

select
eco.s_name,pt.project_name
from kc_billhead kb
LEFT JOIN project_table pt ON kb.tr_proj= pt. pc
LEFT JOIN emp_chg_other eco ON pt.pc=eco.chg_data
LEFT JOIN hr_epm_station hes ON eco.emp_id = hes.ccode and hes.station='0106'
LEFT JOIN fg_ogm_station fos ON hes.station=fos.cno

where kb.billno='CGRK20180102001';

2行合并只能采用临时表了

可以使用 LISTAGG 语句 尝试一下

使用listagg( ) within group ( order by )

前面说的对生成临时表

两行合并成一行,是 s_name1、s_name2 /project_name1、 project_name2 还是 s_name1/project_name1/ s_name2/project_name2?

mysql 可以用 GROUP_CONCAT 函数,oracle用wm_concat函数。例如:
区域 城市
广东 广州
广东 东莞
福建 龙岩
福建 福州
福建 厦门
select 区域, GROUP_CONCAT (城市) from table_name group by 区域
查询结果:
区域 城市
广东 广州,东莞
福建 龙岩,福州,厦门

本地没装MySQL,你试下应该是可以的。另外,多表关联的时候,建议使用 join in+嵌套 这样比较方便看

比如你的可以写成:
select t1.s_name,group_counct(t2.project_name)

from emp_chg_other t1,project_table t2 .......

group by t1.s_name

根据姓名分组,并将相同姓名的数据合并到一行。
如果MYSQL 简称不能 这样 project_table t2 写的话,可以加上AS ( project_table AS t2 ),oracle是可以这样用的

用mysql表连接不就行么???

这个是行列互换,网上例子很多

合并一行?是两个名字拼在一起吗