出錯原因是
select x from tablea where column=(select b from tableb)
而子查詢返回的結果不止一條
按這個情況查詢,有沒有辦法解決不出錯又能查詢到?
select x from tablea where column=any(select b from tableb)
select x from tablea where column in(select b from tableb);
一般 “1 = N”時就會報這個錯,看你的實際用途,是在一個范圍內的話可以采用上述方法,這種錯誤也經常在DML 語句中出現!
引用 1 樓 suiziguo 的回復:
select x from tablea where column=any(select b from tableb)
可以實現
引用 2 樓 adebayor 的回復:
select x from tablea where column in(select b from tableb);
支持.
select x from tablea where column=(select b from tableb where rownum=1)
SQL code
SELECT a.x
FROM tablea a, tableb b
WHERE a.COLUMN = b.b
幾種方法,效率各不同
如2樓
select x from tablea where column in(select b from tableb);
還可以用EXISTS,大部分情況比IN的效率高
select x from tablea a where exists
(select 1 from tableb b
where b.b=a.column
);
1,select x from tablea where column=any(select b from tableb)
2,select x from tablea where column in(select b from tableb);
3,select x from tablea a where exists
(select 1 from tableb b
where b.b=a.column
);
這三種方法,習慣用第三種,不知道具體的執行效率是怎么樣的,請高人指教。