连表查询二-面试笔试

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不出现在英雄表中就可以了。
这个我还是试了好久才想出来的。好好总结经验吧。单表的信息不出现在多表中就可以了。

2.查询你认为等级最厉害的前十个用户信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值