我有兩個表。「單行子查詢返回多行」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
'orders2'中有多行'cust_id' –
無法理解您的期望。首先你使用table'customers',然後你創建另一個表,而不是從'customers'複製,但是使用'orders',然後使用該表的查詢並且說它是「相似的」?這個錯誤很明顯是因爲你的表'orders'中的cust_id'1000000001'和它的副本表'orders2' –
你的第二個查詢在它的子查詢中使用了'orders'的副本。你的第一個使用'客戶'。您的代碼可能相似,但您的數據完全不同。 –