作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了以下表格:
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/
我是一名优秀的程序员,十分优秀!