Sql 选择拥有所有类型自行车的车主

Sql 选择拥有所有类型自行车的车主,sql,database,postgresql,join,subquery,Sql,Database,Postgresql,Join,Subquery,下面是两张表格的图片,车主和车辆。 我需要找出拥有各种自行车的车主。 例子: O_id 100、O_id 101、O_id 102有V_id=1的自行车,但是 O_id 103具有所有类型的自行车V_id=1和V_id=5 如何编写查询以获取这些详细信息? 我的问题是: select o.o_id from owner o,vehicles v where o.v_id = v.v_id where v_type = 'bike' 这显示的是所有拥有自行车的车主,但不是拥有所有自行车的车主,按

下面是两张表格的图片,车主和车辆。 我需要找出拥有各种自行车的车主。 例子: O_id 100、O_id 101、O_id 102有V_id=1的自行车,但是 O_id 103具有所有类型的自行车V_id=1和V_id=5 如何编写查询以获取这些详细信息? 我的问题是:

select o.o_id from owner o,vehicles v where
o.v_id = v.v_id where v_type = 'bike'
这显示的是所有拥有自行车的车主,但不是拥有所有自行车的车主,按您想要获得的o_id分组。 仅取那些拥有相同数量的自行车countv_id的组,这些自行车存在于total select count*中,其中v_type=‘bike’

模式:

create table owner (
    o_id int,
    v_id int
);
create table vehicle (
    v_id int,
    v_type text
);
insert into owner (o_id, v_id) values
(100, 1),
(101, 1),
(102, 1),
(103, 1),
(100, 2),
(101, 3),
(103, 5);

insert into vehicle (v_id, v_type) values
(1, 'Byke'),
(2, 'Car'),
(3, 'Car'),
(4, 'Car'),
(5, 'byke');

以下是使用子查询和联接查询的两个查询:

postgres=# select count(*) as bike_count, O_id  from owner o join vehicle v using(v_id)
postgres-# where  upper(v.V_type) = 'BIKE'
postgres-# group by 2
postgres-# order by 1 desc
postgres-# ;
 bike_count | o_id
------------+------
      2 |  103
      1 |  100
      1 |  102
      1 |  101
(4 rows)
模式:

drop table vehicle;
drop table owner;
create table vehicle(V_id int, V_type varchar(20));
create table owner(O_id int , V_id int);
insert into vehicle values(1,'Bike');
insert into vehicle values(2,'Car');
insert into vehicle values(3,'Car');
insert into vehicle values(4,'Car');
insert into vehicle values(5,'bike');
insert into owner values(100, 1);
insert into owner values(101, 1);
insert into owner values(102, 1);
insert into owner values(103, 1);
insert into owner values(100, 2);
insert into owner values(101, 3);
insert into owner values(103, 5);
子查询:

postgres=# select count(*) as bike_count, O_id  from owner where V_id in (
postgres(#    select V_id from vehicle where upper(V_type) = 'BIKE'
postgres(# )
postgres-# group by 2
postgres-# order by 1 desc
postgres-# ;
 bike_count | o_id
------------+------
      2 |  103
      1 |  101
      1 |  100
      1 |  102
(4 rows)
加入查询:

postgres=# select count(*) as bike_count, O_id  from owner o join vehicle v using(v_id)
postgres-# where  upper(v.V_type) = 'BIKE'
postgres-# group by 2
postgres-# order by 1 desc
postgres-# ;
 bike_count | o_id
------------+------
      2 |  103
      1 |  100
      1 |  102
      1 |  101
(4 rows)

如果只需要所有者,可以将查询限制为1

是否可以使用子查询?老实说,我发现这有点难以理解,因为使用子查询。我在回答中添加了一个解释,如果没有一位车主拥有所有的自行车怎么办?如果问你的问题是让车主拥有所有的自行车,那么如果没有一位车主拥有所有1辆或更多的自行车,那么答案是“无”。正当或者我错过了什么,对。但是您的查询是否没有返回任何内容?我不这么认为。还是我遗漏了什么?是的,是的。只需运行delete from owner,其中V_id在1,5中;然后运行select查询。它不返回任何值。你在开玩笑吧?我投你反对票。它不起作用。
postgres=# select count(*) as bike_count, O_id  from owner o join vehicle v using(v_id)
postgres-# where  upper(v.V_type) = 'BIKE'
postgres-# group by 2
postgres-# order by 1 desc
postgres-# ;
 bike_count | o_id
------------+------
      2 |  103
      1 |  100
      1 |  102
      1 |  101
(4 rows)