简体   繁体   English

选择以获得1列的两个不同查询结果作为2列的一个结果

[英]select to get two different query results with 1 column as one result with 2 columns

I have the two queries which are mostly simple select statements which are in no way related to each other but both returning same number of rows. 我有两个查询,它们大多数都是简单的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

I want the result of both the sqls as the result of 1 select query returning the values of two select statements as 1 result with 2 columns (one for each select statement) 我想要两个sql的结果作为1 select查询的结果,返回两个select语句的值作为2列的1个结果(每个select语句一个)

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

Here is what i tried, but i get duplicates in results 这是我尝试过的方法,但结果重复

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

or 要么

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;

which gives 这使

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

Please Help. 请帮忙。

You can do: 你可以做:

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

However, this does not make much syntatic sense to link the two fields in this way as they are not related. 但是,由于这两个字段不相关,因此以这种方式链接这两个字段在语法上没有太大意义。

If you are just trying to reduce the number of round trips to the database, you could instead use a stored procedure which returns two cursors (one for each query). 如果您只是想减少到数据库的往返次数,则可以使用存储过程来返回两个游标(每个查询一个)。

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;
/

You have to enumerate rows at first somehow, for instance with rownum and then join them using that key: 首先,您必须以某种方式枚举行,例如使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将两个不同的不相关的SQL查询(返回单列结果)组合为一个具有两列的查询结果 - Combine two different unrelated SQL queries( returning single column results) into one query result with two columns 选择查询以通过合并两列来获得结果 - select query to get result by merging two columns sql 合并两个 select 查询结果,如果某一列的值有冲突,则保留查询 A 的结果 - sql merge two select query results, keep result from query A if there is a conflict on one column's value 如何将两个 SQL 查询结果添加到一个结果表中,每个查询结果具有不同的列? - How to add two SQL queries results in one resultant table with different columns for each query result? 将两个SQL SELECT查询结果合并为一个查询和一个结果 - Merge two SQL SELECT queries results into one query and one result 查询合并两列,如果它们不同,否则只返回一列的结果 - Query to combine two columns IF they are different, otherwise just return the result from one column 如何使用sp将两个select查询结果合并为一个结果? - How to combine two select query results into one result using sp? 将结果附加到两个具有不同列的选择查询(显示一个输出)? - Attach result two select queries with different columns (Show one output)? 将两个表中的列合并为一个结果,然后在SELECT查询中使用 - Combine columns from two tables AS one result and THEN use in SELECT query 根据第一个选择查询结果从两个不同的选择查询结果(Oracle)中选择一个值 - Select one value from two different select query result (Oracle) based on first select query result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM