2016-12-30 102 views
1

我有兩個表。「單行子查詢返回多行」SQL的錯誤

CREATE TABLE Customers 
(
    cust_id  char(10) NOT NULL , 
    cust_name char(50) NOT NULL , 
    cust_address char(50) NULL , 
    cust_city char(50) NULL , 
    cust_state char(5) NULL , 
    cust_zip  char(10) NULL , 
    cust_country char(50) NULL , 
    cust_contact char(50) NULL , 
    cust_email char(255) NULL 
); 
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) 
VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', '[email protected]'); 
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact) 
VALUES('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green'); 
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) 
VALUES('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', '[email protected]'); 
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) 
VALUES('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', '[email protected]'); 
INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact) 
VALUES('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard'); 

CREATE TABLE Orders 
(
    order_num int  NOT NULL , 
    order_date date  NOT NULL , 
    cust_id char(10) NOT NULL 
); 
INSERT INTO Orders(order_num, order_date, cust_id) 
VALUES(20005, TO_DATE('2012-05-01', 'yyyy-mm-dd'), '1000000001'); 
INSERT INTO Orders(order_num, order_date, cust_id) 
VALUES(20006, TO_DATE('2012-01-12', 'yyyy-mm-dd'), '1000000003'); 
INSERT INTO Orders(order_num, order_date, cust_id) 
VALUES(20007, TO_DATE('2012-01-30', 'yyyy-mm-dd'), '1000000004'); 
INSERT INTO Orders(order_num, order_date, cust_id) 
VALUES(20008, TO_DATE('2012-02-03', 'yyyy-mm-dd'), '1000000005'); 
INSERT INTO Orders(order_num, order_date, cust_id) 
VALUES(20009, TO_DATE('2012-02-08', 'yyyy-mm-dd'), '1000000001'); 

有趣的是,如果我運行:

SELECT count(*) AS order_count, 
    (SELECT cust_name 
    FROM customers 
    WHERE customers.cust_id=orders.cust_id) AS cust_name, 
    cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

沒有錯誤。

但是,如果我創建一個新表並運行一個類似的代碼:

CREATE TABLE orders2 AS SELECT * FROM orders 
SELECT count(*) AS order_count, 
    (SELECT cust_id 
    FROM orders2 
    WHERE orders2.cust_id=orders.cust_id) AS cust_id2, 
    cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

它給了我一個錯誤:single-row subquery returns more than one row

那麼,爲什麼會出現這種情況?我知道錯誤信息是什麼。根據文檔,如果「外部查詢必須使用關鍵字ANY,ALL,IN或NOT IN中的一個來指定要比較的值,因爲子查詢返回了多行」,則會觸發此錯誤。不滿意。但我不明白爲什麼第一個代碼是合格的,但第二個代碼不合格。

DBMS:甲骨文11

+0

'orders2'中有多行'cust_id' –

+1

無法理解您的期望。首先你使用table'customers',然後你創建另一個表,而不是從'customers'複製,但是使用'orders',然後使用該表的查詢並且說它是「相似的」?這個錯誤很明顯是因爲你的表'orders'中的cust_id'1000000001'和它的副本表'orders2' –

+0

你的第二個查詢在它的子查詢中使用了'orders'的副本。你的第一個使用'客戶'。您的代碼可能相似,但您的數據完全不同。 –

回答

0

我真的不明白失敗的說法,更甚至我不明白爲什麼你需要一個ORDERS2表。 無論如何,我猜你想要的訂單爲每個客戶ID的計數,如果是的話我認爲沒有必要爲子選擇,你可以做

SELECT count(*) AS order_count, cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

還是我失去了一些東西?

0

爲了幫助你理解上的差異:

因爲你似乎並不具備此刻你的客戶表的PK,做到以下幾點:

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact) 
VALUES('1000000005', 'DUPLICATE CUST_ID', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard'); 
  • 然後嘗試運行您的再次查詢。 您應該注意到它現在也失敗,出現相同的錯誤
  • 反之,如果你只是執行第二查詢解析檢查不運行它:你不會得到一個錯誤,因爲它語法正確

So why did this happen?

你的代碼是相似的,但你的數據是不同的。該錯誤是由檢測到數據問題當該查詢是執行觸發。 當查詢僅編譯爲時,無法檢測到此錯誤。

But I can not understand why the first code is eligible but the second code is not.

代碼在這兩個查詢是有效的。只是子查詢在執行某些orders.cust_id值時返回的行太多。

這就是爲什麼你必須構建這樣查詢時要非常小心關係的基數。如果你犯了一個錯誤,查詢可幾年成功運行,當返回一個額外的行,然後突然斷裂。

0

的選擇:

SELECT count(*) AS order_count, 
    (SELECT cust_id 
    FROM orders2 
    WHERE orders2.cust_id=orders.cust_id) AS cust_id2, 
    cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

是differetn的選擇:

SELECT count(*) AS order_count, 
    (SELECT cust_name 
    FROM customers 
    WHERE customers.cust_id=orders.cust_id) AS cust_name, 
    cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

因爲您從訂單第二個表,而不是從Custmer:

CREATE TABLE orders2 AS SELECT * FROM orders 

併線cupplicate與cust_id` = 1000000001

1

多德u有重複的cust_id「1000000001」,以及如何預期SQL打工了。你能否更詳細地闡述你的要求。可以看到他們中許多人不清楚你的問題。

如果有多個(重複的)Cust_id存在,那麼它會帶來這種錯誤信息。

使用鮮明解決了查詢和我沒有得到任何更多的錯誤消息。

SELECT count(*) AS order_count, 
    (SELECT distinct cust_id 
FROM orders2 
WHERE orders2.cust_id=orders.cust_id) AS cust_id2, 
cust_id 
FROM orders 
GROUP BY cust_id 
ORDER BY cust_id; 

請糾正我,如果我的建議是錯誤的,因爲顯然ü應該能夠理解這個問題。

相關問題