我在oracle中編寫sql查詢。我的要求是我想有條件地更新數據庫列的情況下。以下是我使用三張表格的示例數據。爲什麼這個oracle查詢說'單行子查詢返回多行'
create table z_product(uuid int, variationvalue varchar(10));
create table z_listprice(productid int, price int);
create table z_variation(masterproductid int, productid int);
insert into z_product values(1,null);
insert into z_product values(2,null);
insert into z_product values(3,null);
insert into z_product values(4,null);
insert into z_product values(5,null);
insert into z_product values(6,null);
insert into z_listprice values(1,10);
insert into z_listprice values(3,20);
insert into z_listprice values(5,10);
insert into z_listprice values(6,19);
insert into z_variation values(2, 1);
insert into z_variation values(2, 3);
insert into z_variation values(4, 5);
insert into z_variation values(4, 6);
我想如果所有的價值相等與「eqaul」如果價格與「notequal」消息不同的「masterproductid」更新「Variation值」列。我試圖
查詢
UPDATE z_product pr SET pr.variationvalue =
CASE WHEN (SELECT numberofrows FROM (SELECT COUNT(*) AS numberofrows, pricecount , productmasterid
FROM (SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
FROM z_variation prv, z_listprice prl
WHERE prv.masterproductid = (SELECT prv.masterproductid
FROM z_variation prv
WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
AND prl.productid = prv.productid
GROUP BY prl.price, prv.masterproductid)
GROUP BY pricecount , productmasterid)) = '1'
AND
(SELECT pricecount FROM (SELECT COUNT(*) AS numberofrows, pricecount , productmasterid
FROM (SELECT COUNT(prl.price) AS pricecount , prl.price AS price, prv.masterproductid AS productmasterid
FROM z_variation prv, z_listprice prl
WHERE prv.masterproductid = (SELECT prv.masterproductid
FROM z_variation prv
WHERE prv.productid = prl.productid GROUP BY prv.masterproductid)
AND prl.productid = prv.productid
GROUP BY prl.price, prv.masterproductid)
GROUP BY pricecount , productmasterid)) >1
THEN 'equal'
ELSE 'notequal' END
WHERE pr.uuid = (SELECT prv.masterproductid
FROM z_variation prv, z_listprice prl
WHERE prv.productid = prl.productid
AND pr.uuid = prv.masterproductid GROUP BY prv.masterproductid)
什麼是錯的這個查詢。我在哪裏做錯了。任何建議將大大appriciated
@bernie我是新來的sql請糾正我,如果我錯了 – user964147 2013-02-14 23:45:43
道歉,我的意見是口感不佳。我已經刪除它。 – bernie 2013-02-14 23:46:23
錯誤消息表示您的一個或多個子查詢正在返回多個值,因此Oracle不知道要使用哪個值。 – bernie 2013-02-14 23:47:05