Commit 4f1f98ac by ligang

'homework'

parent ed124077
#!/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
#!/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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment