首页 > 数据库 >进入python的世界_day39_数据库——编辑表的补充、SQL数据查询语句

进入python的世界_day39_数据库——编辑表的补充、SQL数据查询语句

时间:2022-11-27 22:02:27浏览次数:39  
标签:group day39 python emp SQL post male where select

一、编辑表的SQL语句补充

alter table 旧表名 rename 新表名; ——改表名
alter table 表名 change 旧字段 新字段 新字段数据类型; ——改字段
alter table 表名 add 新字段 新字段数据类型 after 某个字段 ——指定位置添加字段
alter table 表名 modify 某字段 某字段数据类型 after 另一个字段 ———调整某字段位置到某字段后面
alter table 表名 drop 字段 ——删除指定字段

二、SQL数据查询语句

1.select 与 from

select :

​ >>> 自定义要查询的字段或者字段的啥啥数据(处理后)

from :

​ >>> 指定要查询的来源对象,或许是一张表,或许是多张

# 接下来实操的数据准备,自己复制去mysql 创建好
create table emp(
      id int not null unique auto_increment,
      name varchar(20) not null,
      gender enum('male','female') not null default 'male', #大部分是男的
      age int(3) unsigned not null default 28,
      hire_date date not null,
      post varchar(50),
      post_comment varchar(100),
      salary double(15,2),
      office int, #一个部门一个屋子
      depart_id int
    );
    # 三个部门: 数学、销售、运营
#添加数据
insert into emp(name,gender,age,hire_date,post,salary,office,depart_id) values
    ('jason','male',18,'20170301','浦东第一帅形象代言',7300.33,401,1), #以下是教学部
    ('tom','male',78,'20150302','teacher',1000000.31,401,1),
    ('kevin','male',81,'20130305','teacher',8300,401,1),
    ('tony','male',73,'20140701','teacher',3500,401,1),
    ('owen','male',28,'20121101','teacher',2100,401,1),
    ('jack','female',18,'20110211','teacher',9000,401,1),
    ('jenny','male',18,'19000301','teacher',30000,401,1),
    ('sank','male',48,'20101111','teacher',10000,401,1),
    ('哈哈','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
    ('呵呵','female',38,'20101101','sale',2000.35,402,2),
    ('西西','female',18,'20110312','sale',1000.37,402,2),
    ('乐乐','female',18,'20160513','sale',3000.29,402,2),
    ('拉拉','female',28,'20170127','sale',4000.33,402,2),
    ('僧龙','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
    ('程咬金','male',18,'19970312','operation',20000,403,3),
    ('程咬银','female',18,'20130311','operation',19000,403,3),
    ('程咬铜','male',18,'20150411','operation',18000,403,3),
    ('程咬铁','female',18,'20140512','operation',17000,403,3);

2.where关键字

​ 能跟很多,大多都是判断条件 比如说大小,位置,有没有值等等

  • 某个范围内的ID
    # 查询id 大于等于2小于等于5的数据
    # 方法一:
    select * from emp where id >= 2 and id <= 5;
    
    #方法二::
    select *  from emp where id between 2 and 5;
    
    
  • 超过某个范围的ID
    # 查询 id 小于 3 或者大于6 的数据
    select * from emp where id not between 3 and 6; # 不在3与6 之间的数据
    
  • 按定值筛选做成员运算
    # 查询薪资是20000或者18000 或者17000的数据
    select * from emp where salary in(17000,18000,20000);
    
  • 模糊匹配
    # 查询姓名中带o的员工所有信息
    select * from emp where name like '%o%';
    # 查询姓张的员工的所有信息
    select * from emp where name like '张%';
    
    
  • 按字符数匹配
    # 查询姓名为三位数的员工的所有信息
    select * from emp where name like '___'; # 三个下划线,一个代表一位
    
    # 当然,也能用前天学过的char_length(name)=3 来实现
    

注意:字符长不等于字节长度

  • 查询数据为空的员工的所有信息
    # 注意,不能按 xxx = NULL 这样查询,查不到
    select * from emp where post_comment is NULL;
    

3.group by 关键字

​ 按照一些指定的条件将单个单个数据分为一个个整体

语法: select 分组的依据 或一些其他聚合函数 from 表名 group by 指定的条件

配合分组使用的常见聚合函数

聚合函数 作用
max 最大值
min 最小值
sum 总和
count 计数
avg 平均

​ from 前面的 字段 可以 加 as 跟 名字,这样创出来的新表就是以这个名字作为展示的字段

1.单独用
# 比如说按照 办公室分组 统计有几个办公室
select office as '办公室' from emp group by office;
2.联用
# 以 部门 为单位 分组 获取各组中(各部门) 中的最高薪资
select post as '部门',max(salary) as '薪资' from emp group by post;
>>>
+-----------------------------+------------+
| 部门                        | 薪资       |
+-----------------------------+------------+
| operation                   |   20000.00 |
| sale                        |    4000.33 |
| teacher                     | 1000000.31 |
| 浦东第一帅形象代言          |    7300.33 |
+-----------------------------+------------+
# 每个部门的人数
 select post as '部门',count(id) as '人数' from emp group by post;
>>>
+-----------------------------+--------+
| 部门                        | 人数   |
+-----------------------------+--------+
| operation                   |      5 |
| sale                        |      5 |
| teacher                     |      7 |
| 浦东第一帅形象代言            |      1 |
+-----------------------------+--------+

3.与concat组合用——拼接

​ group_concat(分组之后用) 不仅可以用来显示出分组完字段 还有拼接字符串的作用

​ 括号内可以随意编辑,自由度很高

# 比如 展示部门的个数,以及每个部门的人的姓名
select post,group_concat(name) from emp group by post;
>>>
+-----------------------------+------------------------------------------------+
| post                        | group_concat(name)                             |
+-----------------------------+------------------------------------------------+
| operation                    | 程咬铁,程咬铜,程咬银,程咬金,僧龙               |
| sale                         | 拉拉,乐乐,西西,呵呵,哈哈                       |
| teacher                     | sank,jenny,jack,owen,tony,kevin,tom            |
| 浦东第一帅形象代言            | jason                                          |
+-----------------------------+------------------------------------------------+
# ps:格式看着不舒服把浏览的文本框拉伸一下,就正常了
————————————————————————————————————————————————————————————
select post,group_concat(name,'年龄>>>',age) from emp group by post;

4.having过滤关键字——基于分组再次筛选

#where与 having 的功能其实是一样的 都是用来筛选数据
#只不过 where 用于分组之前的筛选 而 having  用于分组之后的筛选
# 比如说,统计各个办公室,年龄在20岁以上的员工平均工资,并保留平均工资大于5000的办公室
# 一步步分析,第一步:先写年龄在20岁以上的员工这个筛选代码
select * from emp where age > 20;
# 第二步:再写统计办公室分组,最好把分组表的字段名顺便改一下
select office as '办公室' from emp where age > 20 group by office;
# 第三步: 再把平均薪资字段加上
select office as '办公室', avg(salary) as '平均薪资' from emp where age > 20 group by office;
# 第四步: 再次用having筛选 平均工资大于 5000的办公室
select office as '办公室', avg(salary) as '平均薪资' from emp where age >20 group by office having avg(salary) > 5000;
# 完成!
>>>
+-----------+---------------+
| 办公室    | 平均薪资      |
+-----------+---------------+
|       401 | 204780.062000 |
|       403 |  10000.130000 |
+-----------+---------------+
# 用一张表展示不同办公室中,薪资最高的人的姓名和年龄还有薪资
select office as '办公室',max(salary) as '最高工资',group_concat('姓名:',name,':年龄>>>',age) from emp group by office;

5.distinct 去重关键字

select distinct age from emp;
>>>
+-----+
| age |
+-----+
|  18 |
|  78 |
|  81 |
|  73 |
|  28 |
|  48 |
|  38 |
+-----+

6.order by ————排序关键字

select * from emp order by salary asc; #默认升序排
select * from emp order by salary desc; #降序排

#先按照 age降序排,在年轻相同的情况下再按照薪资升序排
select * from emp order by age desc,salary asc;

7.limit——限制展示条数,分页

# 限制 展示条数
select * from emp limit 3;
# 查询工资 最高的人的详细 信息
select * from emp order by salary desc limit 1;

三、多表查询

1.连表操作

select * from emp1,dep1;  #笛卡尔积
# 能直接把两张表拼接到一起,但是一般不用,效率太低 

标签:group,day39,python,emp,SQL,post,male,where,select
From: https://www.cnblogs.com/wznn125ml/p/16930783.html

相关文章