PL/SQL - 选择列值作为字符串数组

发布于 2025-01-02 13:00:06 字数 3001 浏览 1 评论 0原文

我不确定这个问题之前是否在其他地方被问过。我也不知道该怎么说。但我会用一个场景来解释。
我有下表
TAB1 包含列:用户 ID、代码、国家/地区
TAB2 包含列: USERID、CODE、EMAIL

示例内容:

TAB1:
RISHI, A1B2C3, INDIA
RISHI, D2E3F4, INDIA
KANTA, G3H4I5, INDONESIA

TAB2:
RISHI, A1B2C3, [email protected]
RISHI, A1B2C3, [email protected]
RISHI, A1B2C3, [email protected]
RISHI, D2E3F4, [email protected]
RISHI, D2E3F4, [email protected]
KANTA, G3H4I5, [email protected]

我想要从选择查询或 pl/sql 存储过程中得到如下结果:

RISHI, INDIA, A1B2C3, ([email protected], [email protected], [email protected])
RISHI, INDIA, D2E3F4, ([email protected], [email protected])

如果我执行如下选择:

select a.userid, a.code, a.country, b.email
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI';

我得到的结果为:

RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, D2E3F4, [email protected]
RISHI, INDIA, D2E3F4, [email protected]

我基本上需要的是电子邮件 ID 组合在一起形成一个数组。假设 TAB1 包含我实际需要的更多列,但我在本示例中省略了,但 TAB2 只有这三列。

I am not sure if this question is asked anywhere else before. I am not sure how to put it also. But I will explain with a scenario.
I have the following tables
TAB1 with columns : USERID, CODE, COUNTRY
TAB2 with columns : USERID, CODE, EMAIL

Example contents:

TAB1:
RISHI, A1B2C3, INDIA
RISHI, D2E3F4, INDIA
KANTA, G3H4I5, INDONESIA

TAB2:
RISHI, A1B2C3, [email protected]
RISHI, A1B2C3, [email protected]
RISHI, A1B2C3, [email protected]
RISHI, D2E3F4, [email protected]
RISHI, D2E3F4, [email protected]
KANTA, G3H4I5, [email protected]

What I want from a select query or pl/sql stored procedure is a result like this:

RISHI, INDIA, A1B2C3, ([email protected], [email protected], [email protected])
RISHI, INDIA, D2E3F4, ([email protected], [email protected])

If I do a select like :

select a.userid, a.code, a.country, b.email
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI';

I get the result as :

RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, A1B2C3, [email protected]
RISHI, INDIA, D2E3F4, [email protected]
RISHI, INDIA, D2E3F4, [email protected]

What I basically need is the email ids grouped together into an array. Assume that TAB1 contains many more columns which I actually require but I have omitted in this example, but TAB2 has only these three columns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2025-01-09 13:00:06
select a.userid, a.code, a.country, listagg(b.email, ',') within group (order by b.email) as "Emails"
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI'
group by a.userid, a.code, a.country;
select a.userid, a.code, a.country, listagg(b.email, ',') within group (order by b.email) as "Emails"
from tab1.a, tab2.b
where a.userid = b.userid
and a.code = b.code
and a.userid = 'RISHI'
group by a.userid, a.code, a.country;
ま柒月 2025-01-09 13:00:06

我想你想在MySQL中使用GROUP_CONCAT聚合函数。坏消息是 Oracle 没有用于组串联的内置函数,好消息是您可以模拟此类功能。

看一下这个片段:

with data
     as
     (
          select job,
                ename,
                row_number() over (partition by job order by ename) rn,
                count(*) over (partition by job) cnt
      from emp
     )
 select job, ltrim(sys_connect_by_path(ename,','),',') scbp
  from data
  where rn = cnt
  start with rn = 1
  connect by prior job = job and prior rn = rn-1
  order by job

并将返回

JOB       SCBP
--------- ----------------------------------------
ANALYST   FORD,SCOTT
CLERK     ADAMS,JAMES,MILLER,SMITH
MANAGER   BLAKE,CLARK,JONES
PRESIDENT KING
SALESMAN  ALLEN,MARTIN,TURNER,WARD

REFERENCE

I think you want to use the GROUP_CONCAT aggregate function in MySQL. The bad news is Oracle don't have a built-in function for group concactenation and the good news is you can emulated such functionality like that.

Look at this snippet:

with data
     as
     (
          select job,
                ename,
                row_number() over (partition by job order by ename) rn,
                count(*) over (partition by job) cnt
      from emp
     )
 select job, ltrim(sys_connect_by_path(ename,','),',') scbp
  from data
  where rn = cnt
  start with rn = 1
  connect by prior job = job and prior rn = rn-1
  order by job

and will return

JOB       SCBP
--------- ----------------------------------------
ANALYST   FORD,SCOTT
CLERK     ADAMS,JAMES,MILLER,SMITH
MANAGER   BLAKE,CLARK,JONES
PRESIDENT KING
SALESMAN  ALLEN,MARTIN,TURNER,WARD

REFERENCE

瞄了个咪的 2025-01-09 13:00:06
select  listagg(n,  ', ')  
 within group (order by n) con
  from(select department_id d, first_name || ' ' || last_name n from employee 
            where department_id  = 1) ;

CON
---------------------------------------
Alice Perry, Doris Graham, Doris Powell
select  listagg(n,  ', ')  
 within group (order by n) con
  from(select department_id d, first_name || ' ' || last_name n from employee 
            where department_id  = 1) ;

CON
---------------------------------------
Alice Perry, Doris Graham, Doris Powell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文