gpt4 book ai didi

mysql - 如何在MYSQL中获取身份证?

转载 作者:行者123 更新时间:2023-11-29 01:37:26 25 4
gpt4 key购买 nike

我创建了以下表格:

create table people (
ID varchar(9),
name varchar(20),
CONSTRAINT pk_ID PRIMARY KEY (ID)
);

create table cars (
license_plate varchar(9),
ID varchar(9),
CONSTRAINT pk_ID PRIMARY KEY (license_plate)
);

create table accidents (
code varchar(9),
license_plate varchar(9),
CONSTRAINT pk_ID PRIMARY KEY (code)
);

我插入了以下数据:

insert into people(ID, name) values('0x1','Louis');
insert into people(ID, name) values('0x2','Alice');
insert into people(ID, name) values('0x3','Peter');

insert into cars(license_plate, ID) values('001','0x1');
insert into cars(license_plate, ID) values('002','0x2');
insert into cars(license_plate, ID) values('003','0x1');
insert into cars(license_plate, ID) values('004','0x3');

insert into accidents(code, license_plate) values('fd1','001');
insert into accidents(code, license_plate) values('fd2','004');
insert into accidents(code, license_plate) values('fd3','002');

问题是:如何选择他们的车没有出过事故的人?

我的问题是,当我尝试使用 not in 时。在表 accidents 中至少有一辆车是“Louis”,查询显示“Louis”而不应该显示“Louis”。

我的查询:

select ID from people where ID in (select ID from cars where license_plate not in (select license_plate from accidents));

结果:

+-----+
| ID |
+-----+
| 0x1 |
+-----+

最佳答案

select name from people where ID not in (
select distinct c.ID from
accidents as a inner join cars as c
on a.license_plate = c.license_plate
)

说明 = 子查询将加入汽车和事故,将为您提供所有发生事故的汽车的 ID。在此你可以在 people 表上运行 not in 查询

关于mysql - 如何在MYSQL中获取身份证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com