如何在 Oracle 中使用子查询?

Posted

技术标签:

【中文标题】如何在 Oracle 中使用子查询?【英文标题】:How to use subqueries in Oracle? 【发布时间】:2015-07-31 10:40:18 【问题描述】:

我有下表:

table1
-------------------------
 date      |  ID  | name
-------------------------
 13-jul-15 |   1  |  abc
 13-jul-15 |   2  |  abc
 14-jul-15 |   1  |  def
 13-jul-15 |   3  |  abc
 15-jul-15 |   3  |  def
 ...

我要做的是匹配ID并表示如下:

 date1     | name | date2     | name | ID 
------------------------------------------ 
 13-jul-15 | abc  | 14-jul-15 | def  | 1   
 13-jul-15 | abc  |           |      | 2  
 13-jul-15 | abc  | 15-jul-15 | def  | 3
 ...

我使用了以下代码,但没有得到结果。

CREATE PROCEDURE get_details ( oresults1 OUT SYS_REFCURSOR ) AS
BEGIN
  SELECT *
    FROM ((SELECT date, ID FROM table1 WHERE name= "abc") T1
           UNION ALL
          (SELECT date, ID FROM table1 WHERE name= "def") T2 
         )
   WHERE T1.ID= T2.ID
   ORDER BY ID;
END;

我做错了什么?

【问题讨论】:

你想要一个加入而不是一个联合。 【参考方案1】:

这里有几个选择:

with table1 as (select to_date('13/07/2015', 'dd/mm/yyyy') dt, 1 id, 'abc' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 2 id, 'abc' name from dual union all
                select to_date('14/07/2015', 'dd/mm/yyyy') dt, 1 id, 'def' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 3 id, 'abc' name from dual union all
                select to_date('15/07/2015', 'dd/mm/yyyy') dt, 3 id, 'def' name from dual)
-- end of mimicking your table1. See below for the query
select t1.dt date1,
       t1.name name1,
       t2.dt date2,
       t2.name name2,
       t1.id
from   table1 t1
       left outer join table1 t2 on (t1.id = t2.id and t1.name = 'abc' and t2.name = 'def')
where  t1.name = 'abc'
order by t1.id;

DATE1      NAME1 DATE2      NAME2         ID
---------- ----- ---------- ----- ----------
13/07/2015 abc   14/07/2015 def            1
13/07/2015 abc                             2
13/07/2015 abc   15/07/2015 def            3

with table1 as (select to_date('13/07/2015', 'dd/mm/yyyy') dt, 1 id, 'abc' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 2 id, 'abc' name from dual union all
                select to_date('14/07/2015', 'dd/mm/yyyy') dt, 1 id, 'def' name from dual union all
                select to_date('13/07/2015', 'dd/mm/yyyy') dt, 3 id, 'abc' name from dual union all
                select to_date('15/07/2015', 'dd/mm/yyyy') dt, 3 id, 'def' name from dual)
-- end of mimicking your table1. See below for the query
select t1.dt date1,
       t1.name name1,
       t2.dt date2,
       t2.name name2,
       t1.id
from   (select id, dt, name from table1 where name = 'abc') t1
       left outer join (select id, dt, name from table1 where name = 'def') t2 on (t1.id = t2.id)
where  t1.name = 'abc'
order by t1.id;

DATE1      NAME1 DATE2      NAME2         ID
---------- ----- ---------- ----- ----------
13/07/2015 abc   14/07/2015 def            1
13/07/2015 abc                             2
13/07/2015 abc   15/07/2015 def            3

【讨论】:

如何使用to_char(date, 'MM/DD/YYYY HH24:MI:SS')显示日期 您在那里回答了自己的问题?您只需将to_char(date 的“日期”部分替换为您需要转换为该格式的任何日期列(或变量)。例如。在我上面的查询中,您可能会将 t1.dt date1 更改为 to_char(t1.dt, 'MM/DD/YYYY HH24:MI:SS') date1 当我这样做时:select t1.dt date1, t1.name name1, t2.dt date2, t2.name name2, t1.id from table1 t1 left outer join table1 t2 on (t1.id = t2.id and t1.name = 'abc' and t2.name = 'def') where t1.date1 between TO_DATE('13-Jun-2015 13:00:00', 'DD-MON-YYYY HH24:MI:SS') and TO_DATE(13-Jun-2015 13:00:00', 'DD-MON-YYYY HH24:MI:SS) order by date1; 我似乎没有得到记录。我在做什么有问题吗? 我也将t1.dt date1更改为to_char(t1.dt, 'MM/DD/YYYY HH24:MI:SS') date1,如图所示。 您提供的示例数据仅指定了日期,因此时间元素将默认为午夜 - 即。 2015 年 6 月 13 日 00:00:00。假设您使用相同的样本数据,这是否有助于解释为什么您没有看到任何结果?【参考方案2】:

您还可以使用 Oracle 中提供的数据透视函数

WITH table_(date#, id#, name#) AS 
(SELECT to_date('13-jul-15', 'dd-mon-yy'), 1,  'abc' FROM dual UNION all 
 SELECT to_date('13-jul-15', 'dd-mon-yy'), 2,  'abc' FROM dual UNION all
 SELECT to_date('14-jul-15', 'dd-mon-yy'), 1,  'def' FROM dual UNION all
 SELECT to_date('13-jul-15', 'dd-mon-yy'), 3,  'abc' FROM dual UNION all
 SELECT to_date('15-jul-15', 'dd-mon-yy'), 3,  'def' FROM dual)
--------
-- End of data preparation
--------
SELECT * 
  FROM table_
 PIVOT (MIN(date#) AS date#, MIN(name#) AS NAME# FOR name# IN ('abc' AS ABC, 'def' AS DEF));

输出

| ID# |              ABC_DATE# | ABC_NAME# |              DEF_DATE# | DEF_NAME# |
|-----|------------------------|-----------|------------------------|-----------|
|   1 | July, 13 2015 00:00:00 |       abc | July, 14 2015 00:00:00 |       def |
|   2 | July, 13 2015 00:00:00 |       abc |                        |           |
|   3 | July, 13 2015 00:00:00 |       abc | July, 15 2015 00:00:00 |       def |

【讨论】:

以上是关于如何在 Oracle 中使用子查询?的主要内容,如果未能解决你的问题,请参考以下文章

Oracle:如何通过将子查询删除到条件或加入同一张表来提高查询?

如何在oracle中查询某个表的占用了多大的空间!如果是sql语句请讲的详细子我是菜鸟!

在 SQL 子查询中使用多个表进行 Oracle 半联接

如何通过在 oracle 中提供子元素 ID 来获取所有父元素?

如何在 Oracle SQL Developer 中生成带有子查询的 INSERT 语句?

Oracle In子句