Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
homework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ligang
homework
Commits
4f1f98ac
Commit
4f1f98ac
authored
Feb 16, 2019
by
ligang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'homework'
parent
ed124077
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
0 deletions
+145
-0
12-homework-ligang/max_salary.py
+79
-0
12-homework-ligang/top_three.py
+66
-0
No files found.
12-homework-ligang/max_salary.py
0 → 100644
View file @
4f1f98ac
#!/usr/bin/env python3
# -*- coding:utf-8-*-
"""
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+|
|1 | IT |
|2 | Sales |
+----+----------
+编写一个 SQL 查询,找出每个部门工资最高的员工。
例如,根据上述给定的表格,Max 在 IT 部门有最高工资,
Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
"""
"""
1、首先创建数据库 company
create database company` default character set utf8 collate utf8_general_ci;
2、创建数据表:
create table employee (
id int(11) not null auto_increment,
name varchar(64) not null,
salary int(11),
departmentId smallint(5),
primary key (id)
)engine=InnoDB default charset = utf8;
create table department(
id smallint(5) not null,
name varchar(64) not null,
primary key(id)
)engine=InnoDB default charset=utf8;
3、插入数据
insert into department(id,name) values(1,'IT'),(2,'Sales');
insert into employee(name,salary,departmentId) values('Joe',70000,1),('He
nry',80000,2),('Sam',60000,2),('Max',90000,1);
"""
import
pymysql
# pymysql.install_as_MySQLdb()
mydb
=
pymysql
.
Connect
(
host
=
'127.0.0.1'
,
port
=
3306
,
user
=
'root'
,
passwd
=
'root'
,
db
=
'company'
,
charset
=
'utf8'
)
cursor
=
mydb
.
cursor
()
sql
=
"SELECT d.name as department,e.name as employee,max(e.salary) as salary "
\
"FROM department d,employee e "
\
"WHERE d.id = e.departmentId "
\
"GROUP BY e.departmentId;"
cursor
.
execute
(
sql
)
rs
=
cursor
.
fetchall
()
print
(
rs
)
cursor
.
close
()
mydb
.
close
()
\ No newline at end of file
12-homework-ligang/top_three.py
0 → 100644
View file @
4f1f98ac
#!/usr/bin/env python3
# -*- coding:utf-8-*-
"""
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 850000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+|
|1 | IT |
|2 | Sales |
+----+----------
+编写一个 SQL 查询,找出每个部门工资最高的员工。
例如,根据上述给定的表格,Max 在 IT 部门有最高工资,
Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
"""
"""
在原有最高工资的基础上插入两条数据
3、插入数据
insert into employee(name,salary,departmentId) values('Janet',69000,1),('Randy',85000,1);
"""
import
pymysql
# pymysql.install_as_MySQLdb()
mydb
=
pymysql
.
Connect
(
host
=
'127.0.0.1'
,
port
=
3306
,
user
=
'root'
,
passwd
=
'root'
,
db
=
'company'
,
charset
=
'utf8'
)
cursor
=
mydb
.
cursor
()
sql
=
"SELECT e.name,e.salary ,e.departmentId FROM employee e join department d on e.departmentId = d.id "
\
"where ("
\
"select count(distinct e2.salary) from employee e2 where e2.salary > e.salary and e.departmentId = e2.departmentId"
\
") < 3"
\
" order by e.departmentId asc,e.salary desc;"
cursor
.
execute
(
sql
)
rs
=
cursor
.
fetchall
()
print
(
rs
)
cursor
.
close
()
mydb
.
close
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment