码迷,mamicode.com
首页 > 数据库 > 详细

GARENA笔试sql20190926

时间:2019-09-26 23:44:16      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:group   account   排序   name   where   stat   信息   笔试   into   

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;



# 1. 报名的玩家中来自US的玩家名单(account_id,name)
select account_id,name from players where country=‘US‘;

# 2. 每个国家使用M4A1击杀的平均分,按照平均分从大到小排列(country,avg_score)
(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);

# 4. 每个国家使用M4A1击杀得分最高的玩家信息(account_id,name,country,score)



# 5. 每个国家击杀得分最高的2个玩家信息,按照国家,分数的顺序从大到小排序。(account_id,name,country,score)

  

GARENA笔试sql20190926

标签:group   account   排序   name   where   stat   信息   笔试   into   

原文地址:https://www.cnblogs.com/ivyharding/p/11594849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!