---
title: "oracle将查询结果的某一列连接起来"
author: "winhack"
date: "2012-05-10T15:48:24Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2012-05-10T15:48:24Z"
id: 1522181
url: "https://winhack.iteye.com/blog/1522181"
---

如果需要将查询的某一列相连，这时可能需要用到2个东西：

1.sys\_connect\_by\_path

这个的用法是sys\_connect\_by\_path(需要合并的列名,'分隔符')。

仅仅使用这个是不够的，所以还需要递归查询

 

2.递归查询

递归查询的简要语法是 select * from ... where ...  connect by nocycle prior 递归条件 start with 递归起点条件

 

nocycle关键字表示递归将忽略递归中产生的死循环

 

 select max(sys\_connect\_by\_path(username,';')) from   
   (  
   select   
           u.username,rownum ro   
   from   
          t\_base\_user u,t\_base\_role\_user ru   
   where   
         u.id = ru.userid  
    ) newtab   
    start with newtab.ro = 1  
    connect by prior newtab.ro = newtab.ro - 1