create database garena;
use garena;
create table players(
account_id int,
name varchar(20),
country varchar(20)
) ;
desc players;
insert into players values(10001,'N1','US');
insert into players values(10002,'N2','SG');
insert into players values(10003,'N3','US');
insert into players values(10004,'N4','TH');
SELECT * from players;
delete from players where account_id is null;
create table stats(
account_id int,
weapon varchar(20),
score int
) ;
desc stats;
insert into stats values(10001,'GATLING',3);
insert into stats values(10001,'M4A1',5);
insert into stats values(10001,'AN94',1);
insert into stats values(10002,'MP5',1);
insert into stats values(10002,'P90',2);
insert into stats values(10002,'M4A1',2);
SELECT * from stats;
select account_id,name from players where country='US';
(1)先获得B.account_id,weapon,score,country的表
select B.account_id,weapon,score,country from stats B left join players A on A.account_id=B.account_id;
(2)获得每个国家的平均分
select C.country,avg(C.score) as avg_score from
(select B.account_id,weapon,score,country from stats B left join players A on A.account_id=B.account_id) as C
where C.weapon='M4A1' group by country order by avg_score desc;
#3.报名了但是没有参加比赛的玩家名单(account_id,name,country)
#方法1:
select A.account_id,A.name,A.country from players A left join stats B on A.account_id=B.account_id where B.account_id is null;
#方法2:
select account_id, name, country from players where account_id not in (select account_id from stats);
手机扫一扫
移动阅读更方便
你可能感兴趣的文章