学mysql日常
——————————小抄————————
Schema——架构
fetching——获取中
default——预设
truncate table——删除表中所有行
--空格——注释不运行
————————跟课笔记————————
目录:
第三章
- 内连接Inner Joins——交集
- 跨数据库连接(合并)Joining Across Databases
- 自连接Self Joins (例子是员工向谁汇报,总经理无需向谁汇报)
- 多表连接Joining Multiple Tables
- 复合连接条件Compound Join Conditions (join on and)
- 隐式连接语法Implicit Join Syntax(一般不用,会省略链接条件,出错概率up)
- 外连接Outer Joins——并集,left join,right join
- 多表外连接Outer Join Between Multiple Tables
- 自我外部连接Self Outer Joins(例子是员工向谁汇报,总经理无需向谁汇报)
- USING子句The USING Clause (列名要相同)
- 自然连接Natural Joins(一般不用,无法判断电脑怎么链接,出错概率up)
- 交叉连接Cross Joins(噶硬连)
- 联合Unions(把几个结果连在一起,积分对应段位,注意:结果的列数要一致,不然会出错)
第四章
- 列属性Column Attributes——char varchar 【字符 (Character)】
- 插入单行Inserting a Row—— insert into (必填)value(前面顺序),非必填的,有预设值直接打预设值或者打default或者打实际值。
- 插入多行Inserting Multiple Rows—— (),(),()插入多行
- 插入分级行Inserting Hierarchical Rows(有点不太懂????)
- 创建表的副本Creating a Copy of a Table(将查询结果插入到新表insert into+查询(为什么不能create table 再insert into+查询?)将查询结果建为一个新表create table+查询)
- 更新单行(update 表 set乜=乜 where哪个)
- 更新多行Updating Multiple Rows
- 在Updates中用子查询Using Subqueries in Updates
- 删除行Deleting Rows
- 恢复数据库Restoring the Databases

- 1\ The SELECT Statement
use sql_store;
select *
from customers
-- where customer_id = 6
order by birth_date
- 2\ The SELECT Clause (选列)
select
last_name,

first_name,
points,
(points +10)*100 as 'discount factor'
from customers
- 3/ The WHERE Clause (条件)
SELECT *
from customers
where birth_date > '1990-01-01'
- 4/ The AND, OR, and NOT Operators(条件)
SELECT *
from customers
where birth_date >"1990-01-01" and state ="MA"
- 5/ The IN Operator (条件)
select *
from products
where quantity_in_stock in (49,38,72)
- 6/ The BETWEEN Operator (条件)
SELECT * FROM sql_store.customers
where birth_date between "1990-01-01" and "2022-12-31"
- 7/The LIKE Operator (条件)
SELECT * FROM sql_store.customers
where address like '%trail%' or address like '%avenue%'
%——任意(like)
_——一个字符(like)
- 8/ The REGEXP Operator(条件)
SELECT * FROM sql_store.customers
where last_name regexp 'be|tw'
SELECT * FROM sql_store.customers
where last_name regexp '[abd]e'——[]内的字母与[]外的字母任意组合,[]内表达一连串abcd可用a-d
SELECT * FROM sql_store.customers
where first_name regexp "elka|ambur"
^后接查找内容——必需以查找内容为开头(regexp)
查找内容后接$—— 必需以查找内容为结尾 (regexp)
|——or(regexp)
[]——框内与框外任意组合
- 9/The IS NULL Operator(条件)
SELECT * FROM sql_store.orders
where comment is null
- 10/The ORDER BY Operator(排序)
SELECT *, quantity*unit_price as "zongjia"
FROM sql_store.order_items
where order_id =2
order by quantity*unit_price desc
默认升序,desc降序
- 11/The LIMIT Operator(要前几)
SELECT *
FROM sql_store.customers
order by points desc
limit 3

- 12/ Inner Joins (与其他table链接)
join table on table a.columns= table b.columns
select *
from customers c(c是别称)
join orders o(o是别称)
on o.customer_id = c.customer_id
- 13/Joining Across Databases (跨数据库链接)
databases.table.columns
正在使用的是A database,要join B database的数据时,B database的表、列都要加上 B database
的名称,告诉他你要哪个数据库的哪个表的哪个列
- 14/ Self Joins (自链接)
use sql_hr;
SELECT
e.employee_id,
e.first_name,
m.first_name as manager(会产生歧义要重命名)
FROM employees e
join employees m
on e.reports_to = m.employee_id(别链错了)
- 15/Joining Multiple Tables (多表链接)
join table a
on xxx=xxx
join table b
on xxx=xxx
与两个相连差不多
- 16/Compound Join Conditions
复合键用
join
on xxx=xxx
and xxx=xxx
17/Outer Joins
-- orders里面 ordrs id和日期还有客户id我要连客户name,
-- ordrs里面还有shipper id,我要连shipper看一下地址,
-- orders里面仲有status id,我要看一下status是什么
select order_date,
order_id,
first_name,
sh.name as 'shipper name',
os.name as 'status name'
from orders o
left join customers c
on o.customer_id= c.customer_id
left join shippers sh
on o.shipper_id= sh.shipper_id
left join order_statuses os
on o.status =os.order_status_id
————————跟课笔记end——————————————
https://zhuanlan.zhihu.com/p/222968981
↑发现有个笔记写得更好,决定看别人的笔记了哈哈哈
这个翻译的比较好,之前看的那个是机翻,table直接译成桌子……(扶额
————————疑问&找答案————————————————
- 为什么select新增的列,where不适用?
执行顺序,先执行FROM 子句,然后执行WHERE 子句,最后执行SELECT 所以Select 子句后的别名,在where条件中不能使用
所以,以此可以看出,为什么在where语句中没法使用查询列的别名进行过滤了,因为调用where子句的时候,select子句还没有开始执行,所以不识别

- 为什么闪电图标是灰色
按下图解决不了问题,找到服务-mysql发现是启动状态的,不过他给我提供了新思路就是……没有登陆server,一看果然是这个原因

- 为什么一直在fetching?
