当前位置:网站首页>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;
边栏推荐
- Usage of xcolor color in latex
- The examination contents of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge) in 2021 and the free examination questions of the third batch of Guangdong Prov
- Understanding of expectation, variance, covariance and correlation coefficient
- Frrouting BGP protocol learning
- 2022 safety officer-c certificate examination paper and safety officer-c certificate simulated examination question bank
- Introduction to the types and repair methods of chip Eco
- 5. Discrete control and continuous control
- Four digit nixie tube display multi digit timing
- 解决报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- Gnuradio transmits video and displays it in real time using VLC
猜你喜欢
MATLAB R2021b 安装libsvm
Micro rabbit gets a field of API interface JSON
子矩阵的和
液压旋转接头的使用事项
2022 refrigeration and air conditioning equipment operation examination questions and refrigeration and air conditioning equipment operation examination skills
Generic configuration legend
2021 tea master (primary) examination materials and tea master (primary) simulation test questions
General configuration tooltip
5、離散控制與連續控制
qt--將程序打包--不要安裝qt-可以直接運行
随机推荐
2022 low voltage electrician examination content and low voltage electrician simulation examination question bank
Leetcode exercise - Sword finger offer 36 Binary search tree and bidirectional linked list
Matlab code on error analysis (MAE, MAPE, RMSE)
MATLAB R2021b 安装libsvm
小金额炒股,在手机上开户安全吗?
Call (import) in Jupiter notebook ipynb . Py file
用户之声 | 冬去春来,静待花开 ——浅谈GBase 8a学习感悟
Kuntai ch7511b scheme design | ch7511b design EDP to LVDS data | pin to pin replaces ch7511b circuit design
STM32GPIO口的工作原理
Common configurations in rectangular coordinate system
Content of one frame
5. Contrôle discret et contrôle continu
Write a pure handwritten QT Hello World
正则表达式
如果时间是条河
2021-03-06 - play with the application of reflection in the framework
Micro rabbit gets a field of API interface JSON
Kaptcha generates verification code on Web page
Continued from the previous design
About snake equation (2)