数据库操作
登录
mysql -uroot -p
退出
quit/exit
显示数据库版本
show version();
查看当前使用的数据库
select database();
查看所有数据库
show databases;
创建数据库
create database test charset=ut8;
查看创建数据库的语句
show create database test;
使用数据库
use test;
删除数据库
drop database test;
数据库表操作
查看当前数据库中的所有表
show tables;
创建表
int unsigned
无符号整形auto_increment
表示自动增长跟主键在一起not null
表示不能为空primary key
表示主键default
默认值- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
create table students(
id int unsigned primary key auto_increment,
name varchar(30) not null,
age int unsigned,
high decimal(3, 2),
gender enum("男", "女", "保密", "中性") default "保密",
cls_id int unsigned
);
查看表结构
desc students;
查看创建表的语句
show create table students;
修改表
-
添加字段
alter table students add teacher varchar(30);
-
修改字段
alter table students modify teacher varchar(50);
-
重命名
alter table students change teacher squad varchar(30);
-
删除字段
alter table students drop squad;
删除表
drop table students;
增删改查
增加
-
全列插入
insert into students values(1, "张三", 18, 1.83, '男', 1);
-
部分插入
insert into students(name) values("李四"); insert into students(name,age) values("王五", 20);
-
多行插入
insert into students(name) values("赵六"),("李七");
修改
-
全部修改
update students set age=16;
-
按条件修改
update students set age=20 where id=3;
-
按条件修改多个值
update students set age = 15, high=1.80 where id=2;
查询
查询所有
select * from students;
条件查询
select * from students where id = 1;
查询指定列
select name, age from students;
select name, age from students where id = 1;
select students.name,students.age from students;
可以使用as为列或表指定别名
select name as "姓名", age as "年龄" from students;
select name as n, age as a from students;
select s.name, s.age from students as s;
字段的顺序
select age, name from students;
消除重复行
select distinct gender from students;
select gender from students group by gender;
条件查询
比较运算符
-- 查询年纪大于18岁的学生信息
select * from students where age > 18;
-- 查询年纪小于18岁的学生信息
select * from students where age < 18;
-- 查询小于或者等于18岁的学生信息
select * from students where age <= 18;
-- 查询年龄为18岁的所有学生信息
select * from students where age = 18;
-- 查询年龄不为18岁的所有学生信息 != 或者 <>
select * from students where age != 18;
select * from students where age <> 18;
逻辑运算
-- 18和28之间的所有学生信息
select * from students where age > 18 and age < 28; --不包含18 和28
select * from students where age >= 18 and age <= 28; --包含18 和28
select * from students where age between 18 and 28; --包含18 和28
-- 18岁以上的女性的学生信息
select * from students where age > 18 and gender="女";
select * from students where age > 18 and gender=2; --枚举的数据类型可以用数字来表示,数字从1开始
-- 18以上或者身高高过180(包含)以上的学生信息 or
select * from students where age > 18 or height >= 180.00;
-- 不在 18岁以上的女性 这个范围内的信息 not
-- select * from students where not (age>18 and gender=2);
select * from students where not (age > 18 and gender=2);
模糊查询
%
替换任意个,_
替换1个
-- 查询姓名中 以 "小" 开始的名字的学生信息
select * from students where name like "小%";
-- 查询姓名中 有 "小" 所有的名字的学生信息
select * from students where name like "%小%";
-- 查询有2个字的名字的学生信息
select * from students where name like "__";
-- 查询有3个字的名字的学生信息
select * from students where name like "___";
-- 查询至少有2个字的名字的学生信息
select * from students where name like "__%";
范围查询
-- in (1, 3, 8)表示在一个非连续的范围内
-- 查询 年龄为18或34的姓名的学生信息
-- select * from students where age = 18 or age = 34;
select * from students where age in (18, 34); -- (18, 34)不是区间,是确切的值
-- not in 不非连续的范围之内
-- 年龄不是 18或34岁的学生信息
select * from students where age not in (18, 34);
-- between ... and ...表示在一个连续的范围内
-- 查询 年龄在18到34之间的学生信息
select * from students where age between 18 and 34;
-- not between ... and ...表示不在一个连续的范围内
-- 查询 年龄不在18到34之间的学生信息
select * from students where age not between 18 and 34;
空判断
-- 判空is null
-- 查询身高为空的学生信息
select * from students where height is null;
-- 判非空is not null
select * from students where height is not null;
排序
asc
从小到大排列,即升序,desc
从大到小排序,即降序
-- 查询年龄在18到34岁之间的男性,按照年龄从小到大到排序
select * from students where gender=1 and age between 18 and 34 order by age asc;
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序
select * from students where gender=2 and age between 18 and 34 order by height desc;
-- order by 多个字段
-- 查询年龄在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序
select * from students where gender=2 and age between 18 and 34 order by height desc, age asc;
-- 如果年龄也相同那么按照id从大到小排序
select * from students where gender=2 and age between 18 and 34 order by height desc, age asc, id desc;
聚合函数
聚合函数计算的时候不会把null计算进去
-- 查询男性有多少人 count()
select count(*) from students where gender = 1;
-- 查询最大的年龄 max()
select max(age) from students;
-- 最小值 min()
select min(height) from students where gender = 2;
-- 计算所有人的年龄总和 sum()
select sum(age) from students;
-- 计算平均年龄 avg()
select avg(age) from students;
-- 计算平均年龄 sum(age)/count(*)
select sum(age)/count(*) from students;
-- 四舍五入 round(123.23 , 1) 保留1位小数
-- 计算所有人的平均年龄,保留2位小数
select round(avg(age), 2) from students;
-- 计算男性的平均身高 保留2位小数
select round(avg(height), 2) from students where gender = 1; -- 没有算null这种情况 /4
select round(sum(height)/count(*), 2) from students where gender = 1; -- 把null算进去了 /5
分组
公式:select 分组的字段 from 表名 group by 分组字段;
-- group by
-- 按照性别分组,查询所有的性别
select gender from students group by gender;
--# 每种、每类是分组的关键字眼, 以什么为分组,最好在展示的时候,把这个字段放在第一列
-- 计算每种性别中的人数
select gender, count(*) from students group by gender;
-- group_concat(...)
-- 查询同种性别中的姓名group_concat(name)
select gender, group_concat(name) from students group by gender;
-- 查询每组性别的平均年龄
select gender, avg(age) from students group by gender;
-- having(注意having和group by 连用 having后通常也要跟 聚合函数)
-- 注意: 聚合函数如果做为条件出现,只能和having配合。不能和where配合使用。
-- 查询平均年龄超过30岁的性别,以及姓名
select gender, avg(age), group_concat(name) from students group by gender having avg(age) > 30;
-- 查询每种性别中的人数多于2个的信息
-- select gender, count(*) from students group by gender having count(*) > 2;
select gender, count(*) from students group by gender having count(*) > 2;
分页
limit start, count
,limit 放在最后面(注意)。
公式:limit (要显示第几页-1) * 每页分多少个, 每页分多少个;
-- 限制查询出来的数据个数
-- 查询前5个数据
select * from students limit 5;
-- 每页分2个,要显示第1页 (查询前2个数据)
select * from students limit 2;
--通用写法:
select * from students limit 0, 2;
-- 每页分2个,要显示第2页
select * from students limit 2, 2;
-- 每页分2个,要显示第3页
select * from students limit 4, 2;
-- 每页分2个,要显示第4页
select * from students limit 6, 2;
-- 每页分2个,显示第6页的信息, 按照年龄从小到大排序
-- 先整体排序,再进行分页!
select * from students order by age asc limit 10, 2;
连接查询
表与表之间的链接, 为了更好的查出有效数据。
inner join ... on
、select ... from 表A inner join 表B;
。
-- 查询 有能够对应班级的学生以及班级信息
select * from studetns inner join classes on students.cls_id=classes.id;
-- 按照要求显示姓名、班级
select students.name, classes.name from students inner join classes on students.cls_id=classes.id;
-- 给数据表起名字
select s.name, c.name from students as s inner join classes as c on s.cls_id=c.id;
select s.name as "姓名", c.name as "所在班级" from students as s inner join classes as c on s.cls_id=c.id;
-- 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息 students.*,只显示班级名称 classes.name.
select s.*, c.name from students as s inner join classes as c on s.cls_id = c.id;
-- 在以上的查询中,将班级名显示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id;
-- 查询 有能够对应班级的学生以及班级信息, 按照班级名进行排序
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name;
-- 当时同一个班级的时候,按照学生的id进行从小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id = c.id order by c.name, s.id asc;
子查询
一个查询的结果作为另一个查询的一部分, 会把前者的查询称为子查询。
-
标量子查询:子查询返回的结果是一个数据(一行一列)
-
列子查询:返回的结果是一列(一列多行)
-
行子查询:返回的结果是一行(一行多列)
-- 查询出高于平均身高的信息(height)
select avg(height) from students;
select * from students where height > (select avg(height) from students);
-- 查询学生的班级号能够对应的 学生名字
-- select * from students where cls_id in(1,2);
-- select id from classes;
select students.name from students where cls_id = 1 or cls_id = 2;
select students.name from students where cls_id in (1, 2);
select students.name from students where cls_id in (select id from classes);
删除
物理删除
delete from students where id = 4;
逻辑删除(软删除)
alter table students add is_delete bit default 0;
update students set is_delete=1 where id=1;
标签:语句,students,18,数据库,--,MYSQL,where,age,select
From: https://www.cnblogs.com/ilgnefz/p/17218893.html