MySQL实战

task7
mysql> create table if not exists Employee (
    -> Id int not null primary key,
    -> Name VARCHAR(50) not null,
    -> Salary int not null,
    -> DepartmentId int not null
    -> );
mysql> insert into Employee values(1,'Joe',70000,1);
mysql> insert into Employee values(2,'Henry',80000,2);
mysql> insert into Employee values(3,'Sam',60000,2);
mysql> insert into Employee values(4,'Max',90000,1);
mysql> select * from Employee;
+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
|  1 | Joe   |  70000 |            1 |
|  2 | Henry |  80000 |            2 |
|  3 | Sam   |  60000 |            2 |
|  4 | Max   |  90000 |            1 |
+----+-------+--------+--------------+
mysql> create table if not exists Department (
    -> Id int not null primary key,
    -> Name VARCHAR(50) not null
    -> );
mysql> insert into Department values(1,'IT');
mysql> insert into Department values(2,'Sales');
mysql> select * from Department;
+----+-------+
| Id | Name  |
+----+-------+
|  1 | IT    |
|  2 | Sales |
+----+-------+
mysql> select E.Name as Name,E.Salary,D.Name as department
    -> from Employee E join Department D on E.DepartmentId=D.Id
    -> where (E.Salary,E.DepartmentId) in (select max(Salary),DepartmentId from Employee group by DepartmentId);
+-------+--------+------------+
| Name  | Salary | department |
+-------+--------+------------+
| Henry |  80000 | Sales      |
| Max   |  90000 | IT         |
+-------+--------+------------+
task8
mysql> create table if not exists seat (
    -> id int not null primary key,
    -> student VARCHAR(50) not null
    -> );
mysql> insert into seat values(1,'Abbot');
mysql> insert into seat values(2,'Doris');
mysql> insert into seat values(3,'Emerson');
mysql> insert into seat values(4,'Green');
mysql> insert into seat values(5,'Jeames');
mysql> select * from seat;
+----+---------+
| id | student |
+----+---------+
|  1 | Abbot   |
|  2 | Doris   |
|  3 | Emerson |
|  4 | Green   |
|  5 | Jeames  |
+----+---------+
mysql> select id,student from (
    -> select id-1 as id, student from seat where mod(id,2)=0
    -> union
    -> select id+1 as id, student from seat where mod(id,2)=1
    -> and id!=(select count(*) from seat)
    -> union
    -> select id,student from seat where mod(id,2)=1
    -> and id=(select count(*) from seat)
    -> ) seat order by id;
+----+---------+
| id | student |
+----+---------+
|  1 | Doris   |
|  2 | Abbot   |
|  3 | Green   |
|  4 | Emerson |
|  5 | Jeames  |
+----+---------+
task9
mysql> create TABLE scores(
    -> Id int not null,
    -> Score float(3,2)
    -> );
mysql> insert into scores values(1,3.50);
mysql> insert into scores values(2,3.65);
mysql> insert into scores values(3,4.00);
mysql> insert into scores values(4,3.85);
mysql> insert into scores values(5,4.00);
mysql> insert into scores values(6,3.65);
mysql> select Score,
    -> (select count(distinct Score)
    -> from scores where Score>=s.Score) rank
    -> from scores s order by score desc;
+-------+------+
| Score | rank |
+-------+------+
|  4.00 |    1 |
|  4.00 |    1 |
|  3.85 |    2 |
|  3.65 |    3 |
|  3.65 |    3 |
|  3.50 |    4 |
+-------+------+
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值