1.下载pymysql包: pip3 install pymysql
2.编写代码
###### 增加数据 import pymysql # 导入pymysql模块 # 1. 生成conn连接对象 固定语法 conn=pymysql.Connect(host='127.0.0.1', user='root', password="123", database='day40', port=3306,) # 2.创建游标对象,用于操作数据库 cur=conn.cursor() with open('test','r',encoding='utf-8') as f: for line in f: # 学python从开始到放弃 | alex | 人民大学出版社 | 50 | 2018 - 7 - 1 line=line.strip().split('|') # 3 编写sql语句 %s是sql语言的占位符 和python无关 sql='insert into book(b_name,auth, press,price,p_date) values(%s,%s,%s,%s,%s)' # 4. 执行sql语句 , execute方法,可以拼接sql语句 cur.execute(sql,line) # 5. 增, 删, 改 数据 都需要进行提交 conn.commit() # 6 关闭操作游标, 关闭连接 cur.close() conn.close() #### 查询数据 import pymysql # 导入pymysql模块 # 1. 生成conn连接对象 固定语法 conn=pymysql.Connect(host='127.0.0.1', user='root', password="123", database='day40', port=3306,) # 2.创建游标对象,用于操作数据库 cur=conn.cursor() # 3. 编写sql语句 sql = 'select * from book' # 4. 执行sql cur.execute(sql) # 5. 查询结果 res1 = cur.fetchone() # 查询一条 以元组的形式返回数据 res2 = cur.fetchmany(2) # 查询多条 res3 = cur.fetchall() # 查询全部 print(res1) # 控制台打印结果 print(res2) print(res3) # 6 关闭操作游标, 关闭连接 cur.close() conn.close()
where案例
# 1. 条件查询 select emp_name from employee where post='sale'; # 2. 多条件查询 select emp_name , salary from employee where port='teacher' and salary>10000; # 3.关键字 between and select emp_name ,salary from employee where salary beetween 10000 and 20000; # 4.关键字 is null 判断某个字段是不是空,不能用等号 select emp_name,post_comment from employee where post_comment is null; select emp_name,post_comment from employee where post_comment is not null; # 判断是不是 select emp_name,post_comment from employee where post_comment='' ; # 5.关键字IN集合查询 SELECT emp_name,salary FROM employee WHERE salary=3000 OR salary=3500 OR salary=4000 OR salary=9000 ; SELECT emp_name,salary FROM employee WHERE salary IN (3000,3500,4000,9000) ; SELECT emp_name,salary FROM employee WHERE salary NOT IN (3000,3500,4000,9000) ; # 6.关键字LIKE模糊查询 通配符 '%' 多个字符 # 查询eg开头 select * from employee where emp_name like 'eg%'; 通配符 '_' 单个字符 # select * from employee where emp_name like 'al__'; # 7 . regexp 依据正则匹配数据 # 找到以jin开头的数据 select emp_name ,salary*12 from employee where emp_name regexp '^jin' ### 练习 1. 查看岗位是teacher的员工姓名、年龄 # select emp_name , age from employee where post ='teacher'; 2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄 # select emp_name , age from employee where post ='teacher' and age>30; 3. 查看岗位是teacher且薪资在9000-10000范围内的员工姓名、年龄、薪资 # select emp_name , age ,salary*12 from employee where post ='teacher' and salary between 9000 and 10000; 4. 查看岗位描述不为NULL的员工信息 # select * from employee where post_comment is not null; 5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资 # select emp_name , age ,salary from employee where post ='teacher' and salary in (9000,10000,30000); # select emp_name , age ,salary from employee where post ='teacher' and salary=9000 or salary=10000 or salary=30000; 6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资 # select emp_name , age ,salary from employee where post ='teacher' and salary not in (9000,10000,30000); # select emp_name , age ,salary from employee where post ='teacher' and not (salary=9000 or salary=10000 or salary=30000); 7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪 # select emp_name ,salary*12 from employee where post='teacher' and emp_name like 'jin%'; # select emp_name ,salary*12 from employee where post='teacher' and emp_name regexp '^jin'; 特点:
根据某个重复率比较高的字段进行的
一旦分组了就不能对具体的一条数据进行操作
group_concat():只用来做最重的显示,不能作为中间的结果操作其他数据
分组具有去重的效果
### 单独使用group by 关键字分组查询 . 每次操作都是以组的形式操作这些数据 # 按照部门进行分组,获得每个部门的名字 (有去重的效果) select post from employee group by post; ### group by 和 group_concat()函数一起使用 # group_concat()只是用来显示内容 # 按照岗位分组,并查看组内所有成员 select post,group_concat(emp_name) from employee group by post; ### group by 和 聚合函数 一起使用 # 按照岗位分组,并查看每组有多少人 select post,count(id) as count for employee group by post; ##### 强调 1.如果我使用 unique 字段作为分组依据, 则每条记录自成一组,这样没有意义 2. 通常多条记录之间的某个字段值相同,该字段通常用来作为分组的依据 特点:
对一个组进行条件筛选
优先级:
1. 优先级where > group by > having
2.where发生在分组group by之前,where可以有任意字段,where绝对不会和聚合函数一起使用.
3.having发生在分组group by之后,因而having可以使用分组的字段,单无法直接取到其他字段
## 验证 1. mysql> select post from employee where count(salary) group by post; ERROR 1111 (HY000): Invalid use of group function ## 验证 2. #错误,分组后无法直接取到salary字段 mysql> select post,group_concat(emp_name) from employee group by post having salary>1000; ERROR 1054 (42S22): Unknown column 'salary' in 'having clause' #可以使用聚合函数获取未定义字段信息 mysql> select post,group_concat(emp_name) from employee group by post having avg(salary)>10000; +-----------+---------------------------------------------------------+ | post | group_concat(emp_name) | +-----------+---------------------------------------------------------+ | operation | 程咬铁,程咬铜,程咬银,程咬金,张野 | | teacher | 成龙,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex | +-----------+---------------------------------------------------------+ ### 练习 1. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数 # select post, count(id),group_concat(emp_name) from employee group by post having count(id)<2; 3. 查询各岗位平均薪资大于10000的岗位名、平均工资 # select post,avg(salary) from employee group by post having avg(salary)>10000; 4. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资 # select post,avg(salary) from employee group by post having avg(salary)>10000 and avg(salary)<20000; # select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000; count() :统计某个字段出现的次数
max() : 某个字段的最大值
min() : 某个字段的最小值
avg() : 某个字段的平均值
sum() : 某个字段进行求和
### 如果没有进行分组, 那么这张表会作为一个整体成为一组 ### 应用实例 # 统计这张表有多少条记录 select count(*) from employee; # 统计这个表id字段有效值有多少个.(有效值 指的是非空) select count(id) from employee; # 最高的工资 select max(salary) from employee; # 最低的工资 select min(salary) from employee; # 平均工资 select avg(salary) from employee; # 工资总和 select sum(salary) from employee; ### 练习 1. 查询岗位名以及岗位包含的所有员工名字 # select post, group_concat(emp_name) from employee group by post; 2. 查询岗位名以及各岗位内包含的员工个数 # select post, count(emp_name) from employee group by post; 3. 查询公司内男员工和女员工的个数 # select count(emp_name),sex from employee group by sex; 4. 查询岗位名以及各岗位的平均薪资 # select post, avg(salary) from employee group by post ; 5. 查询岗位名以及各岗位的最高薪资 # select post, max(salary) from employee group by post ; 6. 查询岗位名以及各岗位的最低薪资 # select post, min(salary) from employee group by post ; 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资 # select sex, max(salary) from employee group by sex ; # 求各部门薪资大于1w的人的个数 select post,count(id) from employee where salary >10000 group by post; 特点:
1.对单字段, 对多字段进行排序
2.默认升序 从小到大
3.desc 降序 从大到小, asc 升序 从小到大
### 按单列排序 # 升序排序工资 select * from employee order by salary; # asc 升序 select * from employee order by salary asc; # desc 降序 select * from employee order by salary desc; ### 按多列排序: # 先排age 升序 ,再排 薪资 升序 select * from employee order by age , salary ; # 先排age 降序 ,再排 薪资 升序 select * from employee order by age desc , salary ; ### 练习 1. 查询所有员工信息,先按照age升序排序,如果age相同则按照hire_date降序排序 # select * from employee order by age , hire_date desc; 2. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资升序排列 # select post , avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary); 3. 查询各岗位平均薪资大于10000的岗位名、平均工资,结果按平均薪资降序排列 # select post , avg(salary) from employee group by post having avg(salary)>10000 order by avg(salary) desc; 特点:
1.limit(n,m) : n默认从0开始 , 从n+1开始, 取m条
2.与 limit n offset m : 用法一直
3.limit(n) : 取n条
4.和order by 搭配使用
应用:
1.分页
2.限制取值
## 实例 # 降序排序工资,每次取 3 条 select * from employee order by salary desc limit(3); # 从第1条开始,即查出第一条,包含这一条并继续查询5条 select * from employee order by salary desc limit 0,5; # 从第6条开始,即查出第6条,包含这一条并继续查询5条 select * from employee order by salary desc limit 5,5; ### 分页练习 分页显示,每页5条 mysql> select * from employee limit 0,5; +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+ | id | emp_name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+ | 1 | egon | male | 18 | 2017-03-01 | 老男孩驻沙河办事处外交大使 | NULL | 7300.33 | 401 | 1 | | 2 | alex | male | 78 | 2015-03-02 | teacher | NULL | 1000000.31 | 401 | 1 | | 3 | wupeiqi | male | 81 | 2013-03-05 | teacher | NULL | 8300.00 | 401 | 1 | | 4 | yuanhao | male | 73 | 2014-07-01 | teacher | NULL | 3500.00 | 401 | 1 | | 5 | liwenzhou | male | 28 | 2012-11-01 | teacher | NULL | 2100.00 | 401 | 1 | +----+-----------+------+-----+------------+-----------------------------------------+--------------+------------+--------+-----------+ mysql> select * from employee limit 5,5; +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+ | id | emp_name | sex | age | hire_date | post | post_comment | salary | office | depart_id | +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+ | 6 | jingliyang | female | 18 | 2011-02-11 | teacher | NULL | 9000.00 | 401 | 1 | | 7 | jinxin | male | 18 | 1900-03-01 | teacher | NULL | 30000.00 | 401 | 1 | | 8 | 成龙 | male | 48 | 2010-11-11 | teacher | NULL | 10000.00 | 401 | 1 | | 9 | 歪歪 | female | 48 | 2015-03-11 | sale | NULL | 3000.13 | 402 | 2 | | 10 | 丫丫 | female | 38 | 2010-11-01 | sale | NULL | 2000.35 | 402 | 2 | +----+------------+--------+-----+------------+---------+--------------+----------+--------+-----------+ 5 rows in set (0.00 sec) 特点:
mysql可以使用正则进行查询
## 实例 # emp_name字段以al开头的数据 select * from employee where emp_name regexp '^al'; # emp_name字段以on结尾的数据 select * from employee where emp_name regexp 'on$'; # emp_name字段以al开头的数据 select * from employee where emp_name regexp '^al';转载于:https://www.cnblogs.com/dengl/p/11290953.html