我有一个项目,我必须使用嵌套的选择和运算符,如EXISTS,IN,ALL,ANY,来查找拥有更多公寓的业主的名称。
我有2个表所有者和公寓
Owner
"ID" NUMBER(5,0),
"NAME" VARCHAR2(20),
"PHONE" NUMBER(10,0),
CONSTRAINT "PROPR" PRIMARY KEY ("ID")
USING INDEX ENABLE;
Apartment
"ID_AP" NUMBER(5,0),
"ADDRESS" VARCHAR2(35),
"SURFACE" NUMBER(10,0),
"ID" NUMBER(5,0),
CONSTRAINT "APART" PRIMARY KEY ("ID_AP")
USING INDEX ENABLE;
为了找到有多个公寓的业主,我写了这个
SELECT name, id, count(id)
from apartment join owner
using (id)
group by id, name
having count(id)>1
但是如何使用嵌套的Selects和其中一个运算符EXISTS,IN,ALL,ANY?
答案 0 :(得分:0)
例如:
$http({
url: 'http://192.16.1.1:8080/restproj/v1/dealer/car',
method: "POST",
data: { 'message' : $scope.myMessage}
})
答案 1 :(得分:0)
我不太确定你写的查询是否正确。
您提供的正确查询将计算(id_ap)而不是count(id)
SELECT o.id, o.name, count(a.id_ap)
from apartment a
join owner o on o.id = a.id
group by o.id, o.name
having count(a.id_ap)>1
使用in子句的相同查询,但没有公寓数量,你只知道它是2或更多
select o.id, o.name
from owner o
where o.id in (select distinct(a.id) from apartment a
group by a.id having count(a.id_ap) > 1);
您在公寓表引用所有者表时也缺少外键约束。
答案 2 :(得分:0)
WITH myView(name, id,appcount)
as
SELECT name, id, (select count(id) FROM apartment where id = ow.id)
FROM owner ow
SELECT * from myView
WHERE appcount > 2
使用WITH创建内联视图并使用写入查询。 (选择计数(id)FROM公寓,其中id = ow.id)在选择外部查询时将计算公寓数。