在HQL中,如何从子查询中获取逗号分隔的字符串导致select子句?

时间:2016-07-26 07:15:04

标签: oracle hibernate

我要求在HQL中以逗号分隔字符串显示子查询结果。

select DEPARTMENT_ID, 
       (select FIRST_NAME 
          FROM EMPLOYEES 
         where DEPARTMENT_ID =90) as Employee_Names 
 FROM DEPARTMENTS 
where DEPARTMENT_ID =90; 

select子句中的子查询结果我需要显示为逗号分隔值。

提前感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:2)

我看到两种可能的解决方案。

首先

在Oracle中执行:

select
  d.DEPARTMENT_ID,
  listagg(FIRST_NAME, ',') within group(order by FIRST_NAME) as Employee_Name
FROM
  DEPARTMENTS d join EMPLOYEES e
  on d.DEPARTMENT_ID = e.DEPARTMENT_ID
where    
  d.DEPARTMENT_ID = 90
group by
  d.DEPARTMENT_ID

它有局限性:如果串联字符串的总长度> 4000,则代码将失败。

<强>第二

我认为某些报告需要此代码,然后您可以收集所有FIRST_NAME,然后手动阅读ResultSet(如果您的代码在Java中工作)并根据需要合并数据。然后,您可以在Oracle中限制4000个字符。

 select
  d.DEPARTMENT_ID, 
  FIRST_NAME as Employee_Name
FROM
  DEPARTMENTS d join EMPLOYEES e
  on d.DEPARTMENT_ID = e.DEPARTMENT_ID
where
  d.DEPARTMENT_ID =90