PL/SQL - 选择列值作为字符串数组
我不确定这个问题之前是否在其他地方被问过。我也不知道该怎么说。但我会用一个场景来解释。
我有下表
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你想在MySQL中使用
GROUP_CONCAT
聚合函数。坏消息是 Oracle 没有用于组串联的内置函数,好消息是您可以模拟此类功能。看一下这个片段:
并将返回
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:
and will return
REFERENCE