mysql基础篇

这篇博客介绍了MySQL的基础知识,包括基本查询和管理、条件筛选、排序、常见函数、分组、连接查询、子查询、分页以及UNION联合查询。详细讲解了各种操作,如LIKE、BETWEEN、IN、函数应用、连接类型和子查询的用法。
摘要由CSDN通过智能技术生成

mysql 基础篇

1、基本查询和管理

# mysql 连接
mysql -uroot -hlocalhost -P3306 -p

# 查看数据库
show databases;

# 查看当前库中所有表
show tables;

# 查看指定库中所有表
show tables from 库名;

# 显示指定表中所有列
show columns from 表名;

# 打开/使用指定库
use 库名;

# 查询基础
select 查询列表 别名 from 表名;

# 查询去重
select distinct 字段名 from 表名;

# 常见函数
# 显示当前使用数据库
select databases();
# 显示当前用户
select user();
select version();

select ifnull(字段名, 表达式);
select concat(字符1, 字符2, 字符3);
select length(字段名);

# 库和表的管理
create database if not exists 库名;
drop database if exists 库名;

create table if not exists 表名 (表名, 字段类型, 字段约束);
alter table 表名 rename to 新表名;
alter table 表名 add column 新字段名 字段类型;
alter table 表名 modify column 字段名 新字段类型;
drop table if exists 表名;

# 插入数据
insert into 表名(字段名1, 字段名2, 字段名3, ...) values (1,2,3, ...);
insert into 表名 values(1,2,3, ...);
insert into 表名(字段名1, 字段名2, 字段名3, ...) values (1,2,3, ...),(1,2,3, ...);

# 修改数据
update 表名 set 字段名=新值, 字段名=新值, ... where 筛选条件;

# 删除数据
delete from 表名 where 筛选条件;
truncate table 表名;

2、条件查询

select 查询列表
from 表名
where 筛选条件;

2.1、筛选条件

  1. 按条件表达式筛选:>= 、<=、!=、<>、=
  2. 按逻辑表达式筛选:and、or、not
  3. 模糊查询
    like:_任意单个字符、%任意多个字符
    between and
    in
  4. is null

3、排序查询

select 查询列表
from 表名
where 筛选条件
order by 排序列表;

4、常见函数

select 函数名(实参列表);
  • 字符函数
    concat(str1, str2);
    substr(str, pos)
    substr(str, pos, len)
    length(str)
    char_length(str)
    upper(str)
    lower(str)
    trim()
    left(str, len)
    right(str, len)
    lpad(str, substr, len) 左填充
    rpad(str, substr, len)
    strcmp(str1, str2) 比较两个字符的大小
    instr(str, substr) 获取substr在str中第一次出现的索引

  • 数学函数
    cell(x)
    floor(x)
    round(x,d)
    mod(x,y)
    truncate(x,d) 截断
    abs(x) 求绝对值

  • 日期函数
    now()
    curtime()
    curdate()
    date_format(str, format)
    str_to_date(str, format)
    datediff(date1, date2)
    year
    month

  • 流程控制函数

    • if(条件, 表达式1, 表达式2)
    • case 表达式 when 值1 then 结果1 when 值2 then 结果2 else 结果n end
    • case when 条件1 then 结果1 when 条件2 then 结果2 else 结果n end

5、分组函数

sum
avg
max
min
count

6、分组查询

select 分组函数,分组的字段
from 表名
where 分组前的筛选条件
group by 分组列表
having 分组后的筛选列表
order by 排序列表;

7、连接查询

7.1、内连接

¹select 查询列表
²from1 别名
³inner join 表2 别名 on 连接条件
inner join 表3 别名 on 连接条件
⁴where 筛选条件
⁵group by 分组列表
⁶having 分组后筛选
⁷order by 排序列表

执行顺序

from、inner join、where、group by、having、select、order by

7.2、外连接

¹select 查询列表
²from1 别名
³left|right|full join 表2 别名 on 连接条件
⁴where 筛选条件
⁵group by 分组列表
⁶having 分组后筛选
⁷order by 排序列表
limit 分页参数;

8、子查询

在一个select语句中,又嵌套另一个完整的select语句

分类:

  • 放在select后面
  • 放在from后面
  • 放在where或having后面
  • 放在exists后面

特点:

  1. 子查询优先于主查询执行的,主查询使用到了子查询的结果
  2. 放在where或having后面的子查询,一般分两种
    单行子查询:结果集为单行单列,一般搭配关系预算符使用(> >= < <= = <>)
    多行子查询:结果集为多行单列,一般搭配多行操作符使用(any、some、all、in

9、分页查询

select 查询列表
from 表名
where 筛选条件
limit 起始索引, 条目数;

10、union联合查询

完整的select语句1 union
完整的select语句2 union
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值