[英]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.