Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在Oracle中将查询的多个结果连接到一行中_Sql_Oracle_Concatenation - Fatal编程技术网

Sql 在Oracle中将查询的多个结果连接到一行中

Sql 在Oracle中将查询的多个结果连接到一行中,sql,oracle,concatenation,Sql,Oracle,Concatenation,我有两个表,其中一个表通过id引用第一个表 例如,第一个表是具有字段的customer id firstname lastname ------ --------- --------- 1 john smith 2 jessica james id customer_id product descr ------- ------

我有两个表,其中一个表通过id引用第一个表

例如,第一个表是具有字段的customer

    id       firstname     lastname
    ------   ---------     ---------
    1        john          smith
    2        jessica       james
   id        customer_id     product     descr
   -------   -----------     ---------   ------
   1         1               ts          Shirt
   2         1               ti          Tie
   3         2               sk          skrit
例如,第二个表是具有字段的产品

    id       firstname     lastname
    ------   ---------     ---------
    1        john          smith
    2        jessica       james
   id        customer_id     product     descr
   -------   -----------     ---------   ------
   1         1               ts          Shirt
   2         1               ti          Tie
   3         2               sk          skrit
我需要一个将输出以下内容的查询

   customer.firstname  customer.lastname    product_and_desc
   ------------------  ------------------   ---------------------
   john                smith                ts-Shirt , ti-Tie
   jessica             james                sk-skirt
使用每个客户的product rows变量

我感谢你的帮助:)

谢谢,

您可以使用
list\u agg()
。就你而言:

select c.firstname, c.lastname,
       list_agg(p.product||'-'||p.desc, ' , ') within group (order by p.id) as product_and_desc
from customer c join
     product p
     on c.id = p.customer_id
group by c.firstname, c.lastname;

不过,我建议
list_agg()
的第二个参数是“,”而不是“,”。逗号前的空格看起来有点不寻常。

什么版本的Oracle?您好,Oracle版本11.2.0.3.0您尝试过这个吗?我只是尝试了一下,结果成功了:)我不确定我是否应该保留这个问题,似乎在发布之前我没有做好我的研究:$
select first_name,last_name,wm_concat(product||'-'||descr) as product_and_descr
from tbl1 ,tbl2 where tbl1.id=tbl2.customer_id
group by first_name,last_name;