SQL statement basic commands

Daily study notes, please don’t comment if you don’t like them. Corrections and discussions are welcome!


1. MySQL basic commands

SQL语句结束符号
    ;或者/g
退出MySQL
    exit
输出服务器版本
    select version()
输出当前数据库名称
    select database()
输出当前用户名
    select user()
服务器状态指示器
    show status
显示数据库
    show databases;
选择数据库
    use xxx;
显示数据表
    show tables;
获取显示表1中的字段1
    show 字段1 from1;

2. Data Query Language (DQL)

1. Ordinary search

SQL语句结束符号
    ;或者/g
退出MySQL
    exit
输出服务器版本
    select version()
输出当前数据库名称
    select database()
输出当前用户名
    select user()
服务器状态指示器
    show status
显示数据库
    show databases;
选择数据库
    use xxx;
显示数据表
    show tables;
获取显示表1中的字段1
    show 字段1 from1;

2. Sort search

-- 根据表1中的字段1进行排序(order by)
select 字段1 from1 order by 字段1;
    
-- 先根据表1中的字段1排序,再根据字段2排序
select 字段1,字段2 from1 order by 字段1,字段2;
    
-- 根据表1中的字段1降序排序(order by desc)
select 字段1 from1 order by desc;

3. Filter search

(1) where clause operator
检索表1的字段1中符合条件的值1
    select 字段1,字段2 from1 where 字段1=1;
检索表1的字段1中符合条件的值
    select 字段1,字段2 from1 where 字段1>1;
检索字段1中值1不是某条件的值
    select 字段1,字段2 from1 where 字段1!=1;
检索字段1中为null的值
    select 字段1,字段2 from1 where 字段1 is null;
检索字段1中不为null的值
    select 字段1,字段2 from1 where 字段1 is not null;
检索字段1中位于值1和值2两者之间的值
    select 字段1,字段2 from1 where 字段1 between1 and2;
(2) and&or
检索表1中的字段1同时满足条件1和条件2
    select 字段1,字段2 from1 where 字段1>1 and 字段1<2;
检索表1中的字段1满足条件1或者条件2的值
    select 字段1,字段2 from1 where 字段1>1 or 字段1<2;
检索表1中字段1同时满足条件2和条件3,或者满足条件1的信息(优先处理and再处理orselect 字段1,字段2 from1 where 字段1=1 or 字段1=2 and 字段1=3;
检索表1中字段1满足条件1或者满足条件2,并且满足条件3(要先处理or需要加()select 字段1,字段2 from1 where (字段1=1 or 字段1=2) and 字段1=3;
检索字段1中满足指定条件1和条件2的值
    select 字段1,字段2 from1 where1 in (1,2);
检索字段1中不满足指定条件1和条件2的值
    select 字段1,字段2 from1 where1 not in (1,2);
(3) like clause and wildcard %_
检索表1中字段1中以MySQL起头的值(%表示任意字符出现任意次数)
    select 字段1 from1 where 字段1='MySQL%';
检索表1中字段1中包含MySQL的值
    select 字段1 from1 where 字段1='%MySQL%';
检索表1中字段1中第2个字符开始时MySQL的值(_表示一个字符)
    select 字段1 from1 where 字段1='_MySQL%';
检索表1中的字段1中符合正则表达式的值
    select 字段1 from1 where 字段1 regexp '.\\dMySQL';

4.Group search

对表1中的字段1进行分组
    select 字段1 from1 group by 字段1;
先对表1的字段1进行分组,再对条件1进行分组
    select 字段1 from1 group by 字段1 having 条件1;
先根据条件1过滤后再进行分组,再根据条件2进行过滤分组
    select 字段1 from1 where 条件1 group by 字段1 having 条件2;

5. Subquery

先根据表2查询出信息,再根据查询出的信息进行查询
select 字段1, 字段2  
from1  
where 字段3 in (select 字段4 from2 where 条件1);

6.Join table

1和表2连接(内连接inner joinselect1.字段1,2.字段2 from1 join2 on 关联条件;1左连接表2(以左表为主)
    select1.字段1,2.字段2 from1 left join2 on 关联条件;1右连接表2(以右表为主)
    select1.字段1,2.字段2 from1 right join2 on 关联条件;1和表2连接(全外连接full outer joinselect1.字段1,2.字段2 from1 full outer join2 on 关联条件;1自连接,从表1中获取相关信息再在表1查询
    select 别名1.字段1,别名2.字段2 from1 as 别名1,1 as 别名2 on 关联条件;;

7. API in SQL statements

(1) Text processing
将str1和str2拼接在一起
    select concat(str1,str2...);
将表1中的字段1和字段2拼接在一起显示
    select concat(字段1,字段2) from1;
将表1中的字段1使用别名1
    select 字段1 as 别名1 from1;
删除表1中字段1值的空格
    select trim(字段1) from1;
将表1中字段1的值全部转成大写
    select upper(字段1) from1;
将表1中字段1的值全部转成小写
    select lower(字段1) from1;
(2) Date and time
检索表1中字段1中时间为2023-08-26的值
    select 字段1 from1 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) Numerical processing
求表1中字段1的平均值
    select avg(字段1) from1;
求表1中的字段1去重后的平均值
    select avg(distinct 字段1) from1;
对表1中的字段1的数据列数进行计数
    select count(字段1) from1;
对表1中的字段1中的数据去掉null的数据进行计数
    select count(column) from1;
对表1中的字段1中的数据求最大值
    select max(字段1) from1;
对表1中的字段1中的数据求最小值
    select min(字段1) from1;
对表1中的字段1中的数据求和
    select sum(字段1) from1;

8. Grammatical order

select -> from -> where -> group by -> having -> order by -> limit

3. Data Definition Language (DDL)

1. create

创建数据库
    create database 数据库名;
创建数据表
    create table 数据表名(
        id int primary key,
        name varchar(50)
    );

2. alter

修改表结构
向表1中添加字段1
    alter table1 add 字段1 字段类型;
删除表1的字段1
    alter table1 drop 字段1;
修改表1的名字
    alter table1 rename 修改后表名;
修改字段1的名字
    alter table1 change 字段1 字段1修改后名字 修改后字段类型;
修改字段1的数据类型
    alter table1 modify 字段1 数据类型;

3. drop

删除数据库
    drop database if exists 数据库名;
删除数据表
    drop table if exists 数据表名;

4. Data Manipulation Language (DML)

1. insert

向表1中的字段1、字段2插入数据
    insert into1(字段1,字段2) values (数据1,数据2);
向表1中的所有字段插入数据
    insert into1 values(数据1,数据2,数据3...);

2. update

修改符合过滤条件的字段的值
    update1 set 字段1=1,字段2=2 where 过滤条件;
修改全部字段1的所有的值
    update1 set 字段1=1;

3. delete

删除表1中的符合过滤条件的数据
    delete from1 where 过滤条件;

5. Data Control Language (DCL)

查询用户
    select * from user;
创建用户
    create user '用户名'@'主机名' identified by '密码';
修改用户密码
    alter user '用户名'@'主机名' identified with mysql_native_password by '密码';
删除用户
    drop user '用户名'@'主机名';

Guess you like

Origin blog.csdn.net/weixin_57486248/article/details/132548756