ORA-01427单行子查询返回多一行

时间:2014-07-22 07:49:25

标签: sql oracle subquery ora-01427

我遇到了Oracle SQL的问题,错误是: ORA-01427单行子查询返回多一行 我试图找到至少有一辆手动车辆变速器的位置,其里程数低于该位置的任何豪华车辆。

这是我的表

VID          MILEAGE         LOCATION        VSIZE        TRANSMISSION
----------------------------------------------------------------------
V-101       70               AHMD            COMPACT      AUTOMATIC
V-102       50               SURAT           COMPACT      AUTOMATIC
V-103       10               AHMD            MID-SIZE     MANUAL
V-104       30               AHMD            MID-SIZE     AUTOMATIC
V-105       15               VADODARA        FULL-SIZE    AUTOMATIC
V-106       20               AHMD            LUXURY       AUTOMATIC
V-107       50               AHMD            LUXURY       MANUAL

这是我的查询:

SELECT location, transmission, mileage FROM vehicles_workshop 
  WHERE transmission = 'MANUAL' AND mileage = 
      ( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage < 
          (SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
      );

2 个答案:

答案 0 :(得分:0)

而不是等于操作符在子查询

之前使用IN
SELECT location, transmission, mileage FROM vehicles_workshop 
  WHERE transmission = 'MANUAL' AND mileage IN
      ( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage < 
          (SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
      );

用于比较里程数的子查询返回多个值,但等于运算符只能比较单个值,因此会抛出错误。

答案 1 :(得分:0)

SELECT location, transmission, mileage 
FROM vehicles_workshop t1
WHERE transmission = 'MANUAL'         --manual transmission
  AND EXISTS
      ( SELECT *
        FROM vehicles_workshop t2 
        WHERE vsize = 'LUXURY'        --luxury vehicle
        AND t2.location = t1.location --same location
        AND t2.mileage  > t1.mileage) --higher mileage
      ;