[英]How to get the values seperated by comma in a single column using SQL
You can use listagg()
: 您可以使用
listagg()
:
select e.id, e.name, e.sal,
listagg(d.dept, ',') within group (order by d.dept_id) as depts,
listagg(d.dept_id, ',') within group (order by d.dept_id) as dept_ids,
from employee e left join
department d
on e.name = d.name
group by e.id, e.name, e.sal;
Some comments on the data model. 关于数据模型的一些评论。
department
table should have a dept_id
that is the primary key (no duplicates). department
表应具有作为主键的dept_id
(没有重复项)。 department
should really be called employee_departments
because it is a junction table, combining two different entities. department
表实际上应该称为employee_departments
因为它是结合两个不同实体的联结表。 emp_id
as the link to employee
, not name
. emp_id
作为指向employee
的链接,而不是name
。 That is, the foreign key relationship should be to the primary key of employee
. employee
的主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.