选择获取两个不同的查询结果,其中一列为一列,一列为2列

时间:2015-04-07 10:20:05

标签: sql database oracle select

我有两个查询,这些查询大多是简单的select语句,它们彼此没有任何关系,但都返回相同的行数。

SQL> select first_name from employees where rownum <=3;
FIRST_NAME
--------------------
Ellen
Sundar
Mozhe

SQL> select department_name from departments  where rownum <=3;

DEPARTMENT_NAME
------------------------------
Administration
Marketing
Purchasing

我希望两个sqls的结果作为1 select查询的结果,将两个select语句的值作为1个结果返回2个列(每个select语句一个)

FIRST_NAME      DEPARTMENT_NAME
--------------  ------------------
Ellen           Administration
Sundar          Marketing
Mozhe           Purchasing

这是我尝试过的,但我在结果中得到了重复

select first_name, department_name from 
(select first_name from employees where rownum <=5),
(select department_name from departments  where rownum <=5);

with q1 as (select first_name from employees where rownum <=5),
q2 as (select department_name from departments  where rownum <=5)
select first_name, department_name from q1, q2;

给出了

FIRST_NAME           DEPARTMENT_NAME
-------------------- -------------------
Ellen                Administration
Sundar               Administration
Mozhe                Administration
David                Administration
Hermann              Administration
Ellen                Marketing
Sundar               Marketing
Mozhe                Marketing
David                Marketing
Hermann              Marketing
Ellen                Purchasing
Sundar               Purchasing
Mozhe                Purchasing
David                Purchasing
Hermann              Purchasing
Ellen                Human Resources
Sundar               Human Resources
Mozhe                Human Resources
David                Human Resources
Hermann              Human Resources
Ellen                Shipping
Sundar               Shipping
Mozhe                Shipping
David                Shipping
Hermann              Shipping

请帮助。

2 个答案:

答案 0 :(得分:1)

你可以这样做:

WITH indexed_employees AS (
  SELECT rownum AS idx,
         first_name
  FROM   employees
  WHERE  rownum <=3
),
indexed_departments AS (
  SELECT rownum AS idx,
         department_name
  FROM   departments
  WHERE  rownum <=3
)
SELECT e.first_name,
       d.department_name
FROM   indexed_employees e
       INNER JOIN
       indexed_departments d
       ON( e.idx = d.idx )
ORDER BY e.idx

然而,以这种方式链接这两个字段并没有多大的合成意义,因为它们没有关系。

如果您只是想减少到数据库的往返次数,您可以改为使用一个返回两个游标的存储过程(每个查询一个)。

CREATE PROCEDURE get_Top_Emps_and_Depts(
   out_employees_cursor     OUT SYS_REFCURSOR,
   out_departments_cursor   OUT SYS_REFCURSOR
)
AS
BEGIN
  OPEN out_employees_cursor FOR
  SELECT first_name
  FROM   employees
  WHERE  rownum <=3;

  OPEN out_departments_cursor FOR
  SELECT department_name
  FROM   departments
  WHERE  rownum <=3;
END get_Top_Emps_and_Depts;
/

答案 1 :(得分:0)

您必须先以某种方式枚举行,例如使用rownum,然后使用该键加入它们:

with q1 as (select rownum rn, first_name from employees where rownum <=5),
q2 as (select rownum rn, department_name from departments where rownum <=5)
select first_name, department_name from q1 join q2 using (rn);