1.在这里先说下,今天遇到的面试笔试题。虽然看起来简单,但是,总是从不会到会的过程。相信自己,如果能坚持下去,一定会到达某种高度的。
表与表之间的关系是一对多的关系。用户表user,英雄表hero
下面的u_id 与id相关联。id对应多个u_id。
create table user (
id bigint primary key auto_increment,
name varchar(9) not null
);
create table hero(
id bigint primary key auto_increment,
u_id bigint not null,
name varchar(9) not null,
level int(9) not null,
foreign key(u_id) references to user(id)
);
user:
±—±-------+
| id | name |
±—±-------+
| 1 | 大蛇 |
| 2 | 李克 |
| 3 | 木子 |
| 4 | 刘湘 |
±—±-------+
hero:
±—±-----±----------±------+
| id | u_id | name | level |
±—±-----±----------±------+
| 1 | 1 | 盘古 | 1 |
| 2 | 1 | 哪吒 | 1 |
| 3 | 2 | 莫甘娜 | 3 |
| 4 | 3 | 小薇 | 3 |
| 5 | 3 | 天使 | 3 |
±—±-----±----------±------+
题目要求:
1.查询出还没有创建英雄的用户的信息列表。
select user.id,user.name from user where user.name is not null and user.id not in(
select hero.u_id from hero);
#简化之后:
# select * from user where id not in (select u_id from hero);
查询用户表的所有信息,只要id不出现在英雄表中就可以了。
这个我还是试了好久才想出来的。好好总结经验吧。单表的信息不出现在多表中就可以了。