3-数据库mysql

lesson03数据库

 1 #视频1-数据库
 2 1,跟视频添加表student数据    ---mysql(三)高级查询.md
 3 2,修改mysql登陆密码、 ----mysql重置密码.md
 4 3,typora下载
 5 4,web图片墙
 6 127.0.0.1:8888/index    //网页游览  前端页面美食图片
 7 root
 8 123456
 9 
10 mysql> show tables;
11 +-----------------+
12 | Tables_in_juhao |
13 +-----------------+
14 | student         |
15 | student02       |
16 +-----------------+
17 2 rows in set (0.00 sec)
18 
19 mysql> select *from student;
20 +-----------+-----------+-------+-----+-----------+
21 | number    | name      | klass | age | gradName  |
22 +-----------+-----------+-------+-----+-----------+
23 | 201804001 | 刘一      |    19 |  16 | 二年级    |
24 | 201804002 | 陈二      |    18 |  19 | 一年级    |
25 | 201804003 | 张三      |    19 |  20 | 二年级    |
26 | 201804004 | 李四      |    19 |  17 | 一年级    |
27 | 201804005 | 王五      |    19 |  18 | 三年级    |
28 | 201804006 | 赵六      |    18 |  24 | 二年级    |
29 | 201804007 | 孙七      |    19 |  22 | 三年级    |
30 | 201804008 | 周八      |    19 |  21 | 二年级    |
31 | 201804009 | 吴九      |    18 |  25 | 一年级    |
32 | 201804010 | 郑十      |    19 |  23 | 一年级    |
33 | 201804011 | 小周周    |    18 |  20 | 二年级    |
34 | 201804012 | 周周周    |    19 |  21 | 三年级    |
35 +-----------+-----------+-------+-----+-----------+
36 12 rows in set (0.00 sec)
37 
38 
39 
40 --------------------------------------------------
41 #视频2-筛选条件
42 where is null
43 where is not null
44 andornot
45 where age between 18 and 20;//范围的值
46 where age in (18,20);  //里面的值
47 where name like '周%'  //模糊查询  正则匹配  % -任意多个字符
48 where name like '周_'  //模糊查询  正则匹配  _ -任意一个字符
49 select * from student order by  age[asc/desc]  //排序
50 select distinct age from student;  //去重查看
51 
52 
53 #视频3-聚合与分组
54 聚合函数
55 select count(age) from student; 统计个数
56 sum(age)
57 max(age)
58 min(age)
59 avg(age)
60 group_concat(age)  //列出字段全部值
61 
62 分组函数
63 select age from student group by age;
64 select age,max(number) from student group by age;
65 select age,max(number) from student group by age having count(number)=4;  //having 限制输出结果,可以别名
66 select age,max(number) as new_number from student group by age having count(number)=4;  //别名
67 select age as new_age from student where age>3 group by age;
68 执行顺序:where--聚合函数--having
 1 -------------------------------------------------------------
 2 #视频5--pycharm程序操作数据库
 3 限制与分页
 4     select * from student limit 5; //显示5条数据
 5     select * from student limit 5,6; //从第5条后,显示6条数据
 6 
 7     n=1 #第几页
 8     m=5 #5条数据
 9     select * from student limit (n-1)*m,m; //  相当于 limit 0,510 
11 7分钟:--安装pymysql
12     workon py3env     //必须进入环境装
13     pip install --upgrade pip   //升级pip
14     pip install pymysql
15     pip list  //查看装的库
16     deactivate   //退出虚拟环境
17     mysql -uroot -pqwe123
18     
19 pymysql连接数据库:  //本地/远程
20     看-----程序操作数据库.md文件

------------------------------------------------

#03连接mysql.py --写程序:

 1 import pymysql
 2 
 3 db_config = {
 4     'host':'127.0.0.1',
 5     'user':'root',
 6     'password':'qwe123',
 7     'db':'juhao',              #
 8     'charset':'utf8'
 9 }
10 
11 conn = pymysql.connect(**db_config) #连接
12 cursor = conn.cursor()       #生成一个游标
13 
14 sql = "select * from student"
15 result = cursor.execute(sql)            #通过这个方法来执行SQL语句
16 
17 data = cursor.fetchall()        #拿到我们执行sql语句获得的结果
18 
19 for i in data:
20     print(i)
21 
22 cursor.close()
23 conn.close()

#运行:
(201804001, '刘一', 19, 16, '二年级')
(201804002, '陈二', 18, 19, '一年级')
(201804003, '张三', 19, 20, '二年级')
(201804004, '李四', 19, 17, '一年级')
(201804005, '王五', 19, 18, '三年级')
(201804006, '赵六', 18, 24, '二年级')
(201804007, '孙七', 19, 22, '三年级')
(201804008, '周八', 19, 21, '二年级')
(201804009, '吴九', 18, 25, '一年级')
(201804010, '郑十', 19, 23, '一年级')
(201804011, '小周周', 18, 20, '二年级')
(201804012, '周周周', 19, 21, '三年级')

-----------------------------------------

 1 #作业:
 2 从课堂上演示的student表里面
 3 
 4  1. 统计出每个年级分别有多少人
 5  2.统计出每个年级age大于18的人数
 6  3. 统计出一年级的人数
 7  4. 统计出一年级age大于18的人数
 8 
 9 MAX 用程序完成上面的操作
10 
11 mysql> select gradName from student group by  gradName;
12 +-----------+
13 | gradName  |
14 +-----------+
15 | 一年级    |
16 | 三年级    |
17 | 二年级    |
18 +-----------+
19 3 rows in set (0.00 sec)
20 
21 mysql> select count(gradName),gradName from student group by gradName;
22 +-----------------+-----------+
23 | count(gradName) | gradName  |
24 +-----------------+-----------+
25 |               4 | 一年级    |
26 |               3 | 三年级    |
27 |               5 | 二年级    |
28 +-----------------+-----------+
29 3 rows in set (0.00 sec)
30 
31 mysql> select count(age) from student where age>18;
32 +------------+
33 | count(age) |
34 +------------+
35 |          9 |
36 +------------+
37 1 row in set (0.00 sec)
38 
39 mysql> select count(gradName) from student where gradName="一年级";
40 +-----------------+
41 | count(gradName) |
42 +-----------------+
43 |               4 |
44 +-----------------+
45 1 row in set (0.00 sec)
46 
47 mysql> select count(gradName) from student where gradName="一年级" and age>18;
48 +-----------------+
49 | count(gradName) |
50 +-----------------+
51 |               3 |
52 +-----------------+
53 1 row in set (0.00 sec)

猜你喜欢

转载自www.cnblogs.com/tiantiancode/p/12900207.html