我有关于Corelated子查询的简单问题:
表格和数据如下:
CREATE TABLE AUTHORS
(
NAME VARCHAR2(10 BYTE)
);
Insert into AUTHORS Values ('john');
Insert into AUTHORS Values ('bill');
Insert into AUTHORS Values ('dave');
CREATE TABLE BOOKS
(
NAME VARCHAR2(10 BYTE),
PRICE INTEGER
);
Insert into BOOKS Values ('john', 101);
Insert into BOOKS Values ('john', 200);
Insert into BOOKS Values ('john', 300);
insert into books values ('bill', 200 );
Insert into BOOKS Values ('bill', 10);
Insert into BOOKS Values ('dave', 5);
COMMIT;
问题:这里' john'所有书籍的价格> 100。
但是当我使用以下查询时,只返回一行:
select * from
authors a
where 100 < all( select price from books b where a.nAME = b.NAME );
答案 0 :(得分:1)
如果您希望获得具有给定条件的书籍表的所有匹配记录,则下面的查询将起作用
select * from
authors a join books c on a.name=c.name
where 100 < all( select price from books b where a.nAME = b.NAME );
答案 1 :(得分:1)
如果你想要所有书籍的名称,最低价格大于100,我只会这样做:
select b.*
from (select b.*, min(b.price) over (partition by b.name) as minprice
from books b
) b
where minprice > 100;
如果你只想要这个名字,我会使用聚合:
select b.name
from books b
group by b.name
having min(b.price) > 100;
这两个看起来都比你的做法简单。
答案 2 :(得分:0)
如果您要求退还约翰的所有书籍,其价格为&gt; 100那么这应该做到。
SELECT *
FROM authors a
INNER JOIN books b ON a.name = b.name
WHERE a.name = 'john' AND b.price > 100;