Oracle选择子查询返回错误

时间:2016-05-20 11:06:35

标签: oracle subquery

我有一个数据库,其定义如下:

--------------------------------------------------------
--  DDL for Table MAKAN
--------------------------------------------------------

CREATE TABLE "MAMAT"."MAKAN" 
(   "ID" NVARCHAR2(50), 
    "JUDUL_BUKU" NVARCHAR2(45), 
    "NO_ISBN" NVARCHAR2(30), 
    "DT_TERBIT" DATE, 
    "JENIS" NUMBER(1,0), 
    "TEBAL" NUMBER(6,0), 
    "PENGARANG" NVARCHAR2(50)
) SEGMENT CREATION IMMEDIATE 
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
REM INSERTING into MAMAT.MAKAN
SET DEFINE OFF;
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('3F26E5EA2A474778855B28479E01D13C','Di Bawah Lindungan Kabah','3232emasdsdsa',to_date('20/05/2016','DD/MM/RRRR'),1,234,'Mohammad Fajar');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('D0BC61589AB9465AA7D77A2EAE3A9765','JANGAN kau bunuh Pacarmu','dadsadsaddssada',to_date('20/05/2016','DD/MM/RRRR'),1,234,'Mohammad Fajar');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('CE5C935BEB5A4F3CA7627E7C60E5B60F','Lempar Batu Sembunyi Tangan','099999ddddddd',to_date('20/05/2016','DD/MM/RRRR'),2,22,'Mohammad Fajar');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('9347A16BF25D4A85B12629856A9B64C1','Kejar Daku Kau kutangkap','dsdsd',to_date('20/05/2016','DD/MM/RRRR'),1,22,'Mohammad Fajar');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('DD06FD15C06343358B213A4D6EB95D77','Lempar Batu Sembunyi Tangan','099999ddddddd',to_date('20/05/2016','DD/MM/RRRR'),2,22,'Rivai');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('C6199285FDFC4A2FAD93E7E9EC3A339A','Kejar Daku Kau kutangkap','dsdsd',to_date('20/05/2016','DD/MM/RRRR'),2,22,'Rivai');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('C53D8A80726F4AD9B91D43CD60F68332','Kejar Daku Kau kutangkap','dsdsd',to_date('20/05/2016','DD/MM/RRRR'),2,22,'Rivai');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('BE7072DA0BCD4D3FA8A96CAFF38EC0CF','Kejar Daku Kau kutangkap','dsdsd1',to_date('20/05/2016','DD/MM/RRRR'),2,22,'Rivai');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('C81A724896F146F78159BF14F1F1E72B','Kejar Daku Kau kutangkap','mami1',to_date('20/05/2016','DD/MM/RRRR'),3,26,'Jubaidah');
Insert into MAMAT.MAKAN (ID,JUDUL_BUKU,NO_ISBN,DT_TERBIT,JENIS,TEBAL,PENGARANG) values ('0A61A74D4F4941DEBACB134CB637B608','Kejar Daku Kau kutangkap','mami1',to_date('20/05/2016','DD/MM/RRRR'),3,26,'Jubaidah');

我运行以下子查询来检索一些数据:

Select  Ab.Jenis Jn , Ab.Pengarang 
,
(
Select Tebal  From Mamat.Makan Mt Where Mt.Pengarang = Ab.Pengarang 
) 
gg 
From Mamat.Makan Ab
where Ab.Pengarang = 'Mohammad Fajar'   or Ab.Pengarang = 'Rivai' ; 

收到错误:

ORA-01427: single-row subquery returns more than one row
01427. 00000 -  "single-row subquery returns more than one row"
*Cause:    
*Action:

抱歉,我不知道选择子查询的详细实现。我试图通过互联网搜索得到解释。而且我变得沮丧并且坚持太久以至于无法解决这个问题。任何人都可以帮我解决这个问题,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

您的子查询没有意义:您检索的信息已经可以通过主查询提供:

SELECT Ab.Jenis     Jn
     , Ab.Pengarang 
     , Ab.Tebal     gg
  FROM Mamat.Makan Ab
 WHERE Ab.Pengarang IN ( 'Mohammad Fajar', 'Rivai' )
     ;

但是,如果您想为每个不同的Tebal实现提供Pengarang的所有可能值,您可以使用LISTAGG函数(请注意Jenis需要也受到列表聚合的影响,因为它的值在组内不是唯一的):

  SELECT LISTAGG ( Ab.Jenis, ', ' ) WITHIN GROUP ( ORDER BY Ab.Jenis )  Jn
       , Ab.Pengarang 
       , LISTAGG ( Ab.Tebal, ', ' ) WITHIN GROUP ( ORDER BY Ab.Jenis )  gg
    FROM Mamat.Makan Ab
   WHERE Ab.Pengarang IN ( 'Mohammad Fajar', 'Rivai' )
GROUP BY Ab.Pengarang
     ;

答案 1 :(得分:1)

当子查询返回多行时,会发生此错误。子查询本身充当主查询的列值。你应该做的是确保子查询通过添加或修改条件只返回一行