Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
151 lines (82 loc) · 3.02 KB

File metadata and controls

151 lines (82 loc) · 3.02 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

补充知识

inner join、outer join 和 full jion

1. inner join(内连接)

返回两表中连接字段相等的记录。

2. outer join(外连接)

  • left join(左外连接)

    返回左表中所有记录和右边中连接字段相等的记录。

  • right join(右外连接)

    返回右表中所有记录和左边中连接字段相等的记录。

注意:

-- Stu 表(10 条记录)
|-1-|-A-|
|-2-|-B-|
|-3-|-C-|
|-4-|-D-|
|-5-|-E-|
|-6-|-F-|
|-7-|-G-|
|-8-|-H-|
|-9-|-I-|
|-10-|-J-|

-- S 表(5 条记录)
|-1-|-B-|
|-2-|-B-|
|-3-|-B-|
|-4-|-B-|
|-5-|-B-|
SELECT stu.id,stu.name
FROM Stu stu
LEFT JOIN S s
ON stu.name=s.name;

# 返回 14 条记录
# 因为 jion 操作可能会有多条记录

3. full join(全连接)

返回两表中的所有记录和连接字段相等的记录。

子查询和 join 查询

  • 子查询不一定需要 2 个表有关联字段;

    join 查询必须要求有关联字段

  • 数据量比较大时,使用 join 查询寻性能会更好。(因为子查询走的是笛卡尔积)

  • 子查询只有一条记录;

    join 查询可能会有多条记录

    所以将子查询转换为 join 查询时,要注意去重

where 和 on

on 一般与 join 结合使用,使用 join 会产生临时表。

  • on 是在生成临时表时使用的条件

  • where 是在生成临时表后使用的条件

即 on 在 where 之前执行。

where 和 having

  • where

    是一个约束声明,在查询结果返回前对查询结果进行约束;

    where 后面不可以使用 “聚合函数”。

  • having

    是一个过滤声明,在查询结果返回后对查询结果进行过滤;

    where 后面可以使用 “聚合函数”。

order by 和 group by

功能上:

  • order by 是用来排序,分为 desc 和 asc
  • group by 是用来进行分组,并且可以和 "聚合函数" 一起使用

使用上:

group by 在 order by 之前使用,即先进行分组,然后再进行排序。

查询顺序

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

优先级

优先级:not > and > or

可以使用 () 来改变优先级。

数据库备份

1. 备份方式

  • 热备份:当数据库进行备份时,数据库的读写操作均不受影响
  • 温备份:当数据库进行备份时,数据库的读操作可以进行,但不能执行写操作
  • 冷备份:当数据库进行备份时,数据库不能进行读写操作

2. 备份策略

  • 直接复制数据库文件

    针对数据量较小的场景。

  • mysqldump + 复制 binlog

    针对数据量适中的场景。mysqldump 对数据库进行完全备份,定期备份 binlog 达到增量备份的效果。

    mysqldump 实际上就是讲表结构和数据存储在文本文件中,原理:先根据表结构生成 CREATE 语句,然后再将数据转换为 INSERT 语句

  • ivm2 快照 + 复制 binlog

注:binlog 即二进制日志,记录对数据发生或者潜在发生更改的 SQL 语句,以二进制形式保存在文件中。

Morty Proxy This is a proxified and sanitized view of the page, visit original site.