当前位置:网站首页>MySQL数据库(2)
MySQL数据库(2)
2022-07-07 23:47:00 【司小幽】
1.表结构
create table student3(
id int, -- 编号
name varchar(20), -- 姓名
age int, -- 年龄
sex varchar(5),-- 性别
address varchar(100),-- 地址
math int,-- 数学
english int -- 英语
);
insert into student3(id,NAME,age,sex,address,math,english) values(1,'马云',55,'男','杭州',66,78),(2,'马化腾',45,'女','深圳',98,87),(3,'马景涛',55,'男','香港',56,77),(4,'柳岩',20,'女','湖南',76,65),(5,'柳青',20,'男','湖南',86,NULL),(6,'刘德华',57,'男','香港',99,99),(7,'马德',22,'女','香港',99,99),(8,'德玛西亚',18,'男','南京',56,65);
2.查询语句
-- 灵活查询所有数据 部分列数据
-- select * from 表名;
-- select 字段名,字段名 from 表名;
select * from student3;
select id,name from student3;
-- 查询语句是可以带条件的
-- select * from student3 where 条件;
-- 比较运算符 > = >= <= != <>
-- between and
-- in 在。。里面
-- like 模糊查询
-- and 和 or 或者 not
-- 查询数学成绩大于80分的学生
select * from student3 where math > 80;
-- 查询数学成绩小于80分的学生
select * from student3 where math < 80;
-- 查询英语成绩小于80分的学生
select * from student3 where english <= 80;
-- 查询英语成绩大于80分的学生
select * from student3 where english >= 80;
-- between and 在。。区间 包含65,包含80,包头又包尾
-- 查询英语成绩在65和80之间的学生有哪些
-- and两个条件都要满足
select * from student3 where english >= 65 and english <=80;
select * from student3 where english between 65 and 80;
-- or 和 一个条件成立就行
select * from student3 where english >= 80 or english <= 99;
-- not 非 不成立
select * from student3 where not english >= 80;
-- in 在。。里面
-- 英语成绩在80分,90分 english = 80 or english = 90
select * from student3 where english(80,90,70,77);
-- between and 在区间里面 一个范围 english>=65 and english <=80 65<=english<=80
-- in和值相等 english = 80 or english = 90 or english = 70 english=80 english=90
select * from student3;
-- like模糊查询 匹配任意多个字符 查询出来
-- %匹配任意多个字符 _匹配一个字符
-- 查询 姓马的学员信息 马%
select * from student3 where name like '马%';
-- 名字中包含 马 字的学员信息我都查询出来
select * from student3 where name like '%马%';
-- 马字结尾
select * from student3 where name like '%马';
-- 查询 姓马的学员信息 马_
select * from student3 where name like '马__';
select * from student3 where english is NULL;
3.排序
-- order by 字段名 asc 默认是asc 从小到大 降序从大到小
-- 根据年龄进行升序排序
select * from student3 order by age;
select * from student3 order by age desc;
-- 年龄升序 数学成绩降序可以吗?
select * from student3 order by age asc,math desc;
4.聚合函数
-- 一列一列 根据一列的值返回结果
-- max(列名)取最大值
-- min(列名)取最小值
-- avg(列名)取平均值
-- count(列名)统计这列有多少数据
-- sum(列名)求和
-- 查询年龄最大的学员 一个数据
select * from student3;
select max(age) from student3;
select min(age) from student3;
select avg(math) from student3;
-- count(列名)不统计为null的
select count(id) from student3;
select count(*) from student3;
select count(1) from student3;
select sum(math) from student3;
5.分组
-- group by会和聚合函数一起使用
-- group by不会单独使用,单独使用没有意义
-- group by 分组
-- 分组 分成男女两组 男 和 女 数据只有2行
select * from student3 group by sex;
-- 统计男女各有多少人 统计 取个别名 as 用空格
select count(sex) as '统计',sex '性别' from student3 group by sex;
-- 男女英语成绩平均值
select avg(english),sex from student3 group by sex;
-- 查询年龄在28岁(包含)以上的学员人数 按照性别分组
select count(sex) as '统计',sex from student3 where age>=28 group by sex;
-- 查询年龄在28岁(包含)以上的学员人数 按照性别分组,查询性别人数大于2的数据
-- 分组完成之后才能做这个条件
select count(sex) as '统计',sex from student3 where age>=28 group by sex having count(sex)>2;
-- where和having都是加条件
-- where是分组之前过滤数据 不能加聚合函数
-- having是分组之后过滤数据
6.分页
-- limit限制一页有多少条数据
-- limit起始数 从0开始 总共显示多少条数据
-- 只想显示3条数据 显示4,5,6 3,3
select * from student3 limit 1,3;
-- 只写一个数字
select * from student3 limit 3;
select * from student3 limit 3,3;
7.连接查询
-- 外键foreign key 主键 primary key
-- 员工表 编号、姓名、 年龄 主键 编号唯一不会重复 外键对应另外表的主键 通过外键能找到对应的信息
-- 部门表 编号、名称 主键 编号唯一不会重复
create table dept(
id int primary key auto_increment,
name varchar(20)
)charset=utf8;
create table emp (
id int primary key auto_increment,
name varchar(10),
gender char(1), -- 性别
salary double, -- 工资
join_date date, -- 入职日期
dept_id int,
foreign key (dept_id) references dept(id))charset=utf8; -- 外键,关联部门表(部门表的主键) )
insert into dept values(1,'研发部'),(2,'测试部'),(3,'运维部'),(4,'销售部');
insert into emp(name,gender,salary,join_date,dept_id) values('孙悟空','男',7200,'2013-02-24',1);
insert into emp(name,gender,salary,join_date,dept_id) values('猪八戒','男',3600,'2010-12-02',2);
insert into emp(name,gender,salary,join_date,dept_id) values('唐僧','男',9000,'2008-08-08',2);
insert into emp(name,gender,salary,join_date,dept_id) values('白骨精','女',5000,'2015-10-07',3);
insert into emp(name,gender,salary,join_date,dept_id) values('蜘蛛精','女',4500,'2011-03-14',1);
select * from emp;
select * from dept;
-- 要求是查询出员工信息,员工的编号,姓名,性别,工资,入职日期还有部门名称
-- from什么表 带条件
-- 隐式查询
select emp.id,emp.name,gender,salary,join_date,dept.name from emp,dept where emp.dept_id = dept.id;
-- a表 inner join on b表 on条件
select emp.id,emp.name,gender,salary,join_date,dept.name from emp inner join dept on emp.dept_id = dept.id;
-- a表left join b表 on 条件 以左表为基准,左边表的数据全部显示出来。
select emp.id,emp.name,gender,salary,join_date,dept.name from emp left join dept on emp.dept_id = dept.id;
-- a表right join b表 on 条件
select emp.id,emp.name,gender,salary,join_date,dept.name from emp right join dept on emp.dept_id = dept.id;
边栏推荐
- How does Matplotlib generate multiple pictures in turn & only save these pictures without displaying them in the compiler
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- Scalar / vector / matrix derivation method
- Transportation, new infrastructure and smart highway
- Matlab method is good~
- Multi purpose signal modulation generation system based on environmental optical signal detection and user-defined signal rules
- Euler Lagrange equation
- 5、离散控制与连续控制
- Deep learning website
- How to get the first and last days of a given month
猜你喜欢

ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!

Transportation, new infrastructure and smart highway

2022 safety officer-a certificate free examination questions and safety officer-a certificate mock examination

Content of one frame

写一个纯手写的qt的hello world

General configuration toolbox

Problems of font legend and time scale display of MATLAB drawing coordinate axis

2022 high voltage electrician examination skills and high voltage electrician reexamination examination

COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况---材料的添加

Parade ps8625 | replace ps8625 | EDP to LVDS screen adapter or screen drive board
随机推荐
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
4. Apprentissage stratégique
A little experience from reading "civilization, modernization, value investment and China"
Kuntai ch7511b scheme design | ch7511b design EDP to LVDS data | pin to pin replaces ch7511b circuit design
Redis集群
Apt get error
redis的持久化方式-RDB和AOF 两种持久化机制
Talk about smart Park
项目经理有必要考NPDP吗?我告诉你答案
About snake equation (2)
Redis master-slave replication
2022 operation certificate examination for main principals of hazardous chemical business units and main principals of hazardous chemical business units
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
AttributeError: ‘str‘ object has no attribute ‘strftime‘
Anaconda3 tutorial on installing and adding Tsinghua image files
Coordinate conversion of one-dimensional array and two-dimensional matrix (storage of matrix)
npm 內部拆分模塊
GBASE观察 | 数据泄露频发 信息系统安全应如何守护
How does Matplotlib and PIL image integrate and save multiple pictures into one picture
Matlab code on error analysis (MAE, MAPE, RMSE)