日常学习笔记,不喜勿喷,欢迎纠错与探讨!
一、MySQL基本命令
SQL语句结束符号
;或者/g
退出MySQL
exit
输出服务器版本
select version()
输出当前数据库名称
select database()
输出当前用户名
select user()
服务器状态指示器
show status
显示数据库
show databases;
选择数据库
use xxx;
显示数据表
show tables;
获取显示表1中的字段1
show 字段1 from 表1;
二、数据查询语言(DQL)
1.普通检索
SQL语句结束符号
;或者/g
退出MySQL
exit
输出服务器版本
select version()
输出当前数据库名称
select database()
输出当前用户名
select user()
服务器状态指示器
show status
显示数据库
show databases;
选择数据库
use xxx;
显示数据表
show tables;
获取显示表1中的字段1
show 字段1 from 表1;
2.排序检索
-- 根据表1中的字段1进行排序(order by)
select 字段1 from 表1 order by 字段1;
-- 先根据表1中的字段1排序,再根据字段2排序
select 字段1,字段2 from 表1 order by 字段1,字段2;
-- 根据表1中的字段1降序排序(order by desc)
select 字段1 from 表1 order by desc;
3.过滤检索
(1) where子句操作符
检索表1的字段1中符合条件的值1
select 字段1,字段2 from 表1 where 字段1=值1;
检索表1的字段1中符合条件的值
select 字段1,字段2 from 表1 where 字段1>值1;
检索字段1中值1不是某条件的值
select 字段1,字段2 from 表1 where 字段1!=值1;
检索字段1中为null的值
select 字段1,字段2 from 表1 where 字段1 is null;
检索字段1中不为null的值
select 字段1,字段2 from 表1 where 字段1 is not null;
检索字段1中位于值1和值2两者之间的值
select 字段1,字段2 from 表1 where 字段1 between 值1 and 值2;
(2) and&or
检索表1中的字段1同时满足条件1和条件2
select 字段1,字段2 from 表1 where 字段1>值1 and 字段1<值2;
检索表1中的字段1满足条件1或者条件2的值
select 字段1,字段2 from 表1 where 字段1>值1 or 字段1<值2;
检索表1中字段1同时满足条件2和条件3,或者满足条件1的信息(优先处理and再处理or)
select 字段1,字段2 from 表1 where 字段1=值1 or 字段1=值2 and 字段1=值3;
检索表1中字段1满足条件1或者满足条件2,并且满足条件3(要先处理or需要加())
select 字段1,字段2 from 表1 where (字段1=值1 or 字段1=值2) and 字段1=值3;
检索字段1中满足指定条件1和条件2的值
select 字段1,字段2 from 表1 where 值1 in (值1,值2);
检索字段1中不满足指定条件1和条件2的值
select 字段1,字段2 from 表1 where 值1 not in (值1,值2);
(3) like子句和通配符%_
检索表1中字段1中以MySQL起头的值(%表示任意字符出现任意次数)
select 字段1 from 表1 where 字段1='MySQL%';
检索表1中字段1中包含MySQL的值
select 字段1 from 表1 where 字段1='%MySQL%';
检索表1中字段1中第2个字符开始时MySQL的值(_表示一个字符)
select 字段1 from 表1 where 字段1='_MySQL%';
检索表1中的字段1中符合正则表达式的值
select 字段1 from 表1 where 字段1 regexp '.\\dMySQL';
4. 分组检索
对表1中的字段1进行分组
select 字段1 from 表1 group by 字段1;
先对表1的字段1进行分组,再对条件1进行分组
select 字段1 from 表1 group by 字段1 having 条件1;
先根据条件1过滤后再进行分组,再根据条件2进行过滤分组
select 字段1 from 表1 where 条件1 group by 字段1 having 条件2;
5. 子查询
先根据表2查询出信息,再根据查询出的信息进行查询
select 字段1, 字段2
from 表1
where 字段3 in (select 字段4 from 表2 where 条件1);
6. 连接表
表1和表2连接(内连接inner join)
select 表1.字段1,表2.字段2 from 表1 join 表2 on 关联条件;
表1左连接表2(以左表为主)
select 表1.字段1,表2.字段2 from 表1 left join 表2 on 关联条件;
表1右连接表2(以右表为主)
select 表1.字段1,表2.字段2 from 表1 right join 表2 on 关联条件;
表1和表2连接(全外连接full outer join)
select 表1.字段1,表2.字段2 from 表1 full outer join 表2 on 关联条件;
表1自连接,从表1中获取相关信息再在表1查询
select 别名1.字段1,别名2.字段2 from 表1 as 别名1,表1 as 别名2 on 关联条件;;
7. SQL语句中的API
(1) 文本处理
将str1和str2拼接在一起
select concat(str1,str2...);
将表1中的字段1和字段2拼接在一起显示
select concat(字段1,字段2) from 表1;
将表1中的字段1使用别名1
select 字段1 as 别名1 from 表1;
删除表1中字段1值的空格
select trim(字段1) from 表1;
将表1中字段1的值全部转成大写
select upper(字段1) from 表1;
将表1中字段1的值全部转成小写
select lower(字段1) from 表1;
(2) 日期时间
检索表1中字段1中时间为2023-08-26的值
select 字段1 from 表1 where data(字段1)='2023-08-26';
返回当前时间的日期和时间
select now(); --> 2023-08-26 10:02:43
返回一个时间的日期
select date(now()); --> 2023-08-26
返回一个时间的年份
select year(now()); --> 2023
返回一个时间的月份
select month('2023-08-26'); --> 8
返回一个时间的天数
select day(now()); --> 26
返回当前时间
select time(now()); --> 10:02:43
返回当前日期的日期
select curdate(); --> 2023-08-26
(3) 数值处理
求表1中字段1的平均值
select avg(字段1) from 表1;
求表1中的字段1去重后的平均值
select avg(distinct 字段1) from 表1;
对表1中的字段1的数据列数进行计数
select count(字段1) from 表1;
对表1中的字段1中的数据去掉null的数据进行计数
select count(column) from 表1;
对表1中的字段1中的数据求最大值
select max(字段1) from 表1;
对表1中的字段1中的数据求最小值
select min(字段1) from 表1;
对表1中的字段1中的数据求和
select sum(字段1) from 表1;
8. 语法顺序
select -> from -> where -> group by -> having -> order by -> limit
三、数据定义语言(DDL)
1. create
创建数据库
create database 数据库名;
创建数据表
create table 数据表名(
id int primary key,
name varchar(50)
);
2. alter
修改表结构
向表1中添加字段1
alter table 表1 add 字段1 字段类型;
删除表1的字段1
alter table 表1 drop 字段1;
修改表1的名字
alter table 表1 rename 修改后表名;
修改字段1的名字
alter table 表1 change 字段1 字段1修改后名字 修改后字段类型;
修改字段1的数据类型
alter table 表1 modify 字段1 数据类型;
3. drop
删除数据库
drop database if exists 数据库名;
删除数据表
drop table if exists 数据表名;
四、数据操纵语言(DML)
1. insert
向表1中的字段1、字段2插入数据
insert into 表1(字段1,字段2) values (数据1,数据2);
向表1中的所有字段插入数据
insert into 表1 values(数据1,数据2,数据3...);
2. update
修改符合过滤条件的字段的值
update 表1 set 字段1=值1,字段2=值2 where 过滤条件;
修改全部字段1的所有的值
update 表1 set 字段1=值1;
3. delete
删除表1中的符合过滤条件的数据
delete from 表1 where 过滤条件;
五、数据控制语言(DCL)
查询用户
select * from user;
创建用户
create user '用户名'@'主机名' identified by '密码';
修改用户密码
alter user '用户名'@'主机名' identified with mysql_native_password by '密码';
删除用户
drop user '用户名'@'主机名';