目录

  • 数据库管理工具采用 Navicat
  • 选择语句 SELECT / FROM / WHERE / ORDER BY
    • 选择子句 -- AS / DISTINCT
    • 练习1
  • 运算符
    • < 、 <= 、 > 、 >=、 = 、 != (<>)
    • AND、OR 、NOT
    • 练习
    • IN
    • BETWEEN
    • LIKE (% 表示任意字符数 ,下划线 _ 表示一个单字符)搜索字符不区分大小写
    • REGEXP (正则表达式 regular repression) 搜索字符串
    • NULL 获取缺失属性
  • ORDER BY 子句
  • LIMIT 子句
    • 总结:子句顺序
  • 内连接 Inner Joins
    • 连接多表列
    • 跨数据库链接
    • 自连接
    • 连接多表
    • 复合连接条件 (两列确定一条信息)
    • 隐式连接语法 -- 存在交叉连接隐患
  • 外连接 Outer Joins -- left join; right join
    • 多表外连接
    • 自外连接
    • USING -- 相同列名
  • 自然连接 natural Joins
  • 交叉连接 cross Joins
  • 联合 union
  • 行操作
    • 小问题:删除记录后自增id不连续
    • 添加单行
    • 添加多行
    • 分层行 ---- last_insert_id()
    • 创建表复制 子查询
    • 更新单行 ---- update;set
    • 更新多行
    • update 使用 子查询
    • 删除行 --- delete from
  • 数据汇总查询
    • 聚合函数
    • 数据分组(单列,多列)统计
      • 单列
      • 多列
      • having
      • rollup 仅mysql存在
  • 传送门

数据库管理工具采用 Navicat

解决方法

选择语句 SELECT / FROM / WHERE / ORDER BY

select *  -- 返回所有列
from customers -- customers 表中
-- where customer_id = 1
-- ORDER BY first_name

选择子句 – AS / DISTINCT

SELECTfirst_name,last_name,points,( points + 10 )* 100 AS result_data --( points + 10 )* 100 AS "result data"
FROM customers

关键字 AS : 起别名

select distinct state from customers
-- 选择一列时,可用 关键字 DISTINCT 剔除重复项

练习1

SELECT NAME, unit_price,( unit_price * 1.1 ) AS "new price"
FROMproducts

运算符

< 、 <= 、 > 、 >=、 = 、 != (<>)

SELECT   *
FROM customers
WHERE birth_date > '1990-01-01' -- 日期标准表示方法
ORDER BY    birth_date

AND、OR 、NOT

 AND 的优先级大于 OR

NOT 作用:

WHERE birth_date <= '1990-01-01' and points <= 1000
等效于
WHERE not (birth_date > '1990-01-01' or points > 1000)

练习

SELECT   *
FROM    order_items
WHERE   order_id = 6   AND ( quantity * unit_price )> 30

IN

select * from customers
-- where state in('GA','CA','VA') 等效于
where state = 'GA'  OR state = 'CA' or state = 'VA'
select * from products
where quantity_in_stock in (49,38,72)

BETWEEN

select * from customers
-- where points >= 1000 and points <= 3000  等效于
where points between 1000 and 3000
select * from customers
where birth_date between '1990-01-01' and '2000-01-01'
order by birth_date

LIKE (% 表示任意字符数 ,下划线 _ 表示一个单字符)搜索字符不区分大小写

select * from customers
where address like '%trail%' or address like '%avenue%' or phone like '%9'

REGEXP (正则表达式 regular repression) 搜索字符串

^ 表示头部  ^fir 字符串以 fir 开头
$ 表示尾部  fir$ 字符串以 fir 结尾
| 表示多个搜寻模式  fir|mac 字符串包含 fir 或 mac
[] 搜索括号内列举的单字符 [gim]e 表示搜索 ge,ie,me ; [a-h]e表示搜索 ae,be,ce,...,he
select * from customers
-- where first_name = 'elka' or first_name =  'ambur'
-- where first_name regexp 'elka|ambur'
-- where last_name regexp 'ey$|on$'
-- where last_name regexp 'se|^my'
-- where last_name regexp 'br|bu'
-- where last_name regexp 'b[ru]'

NULL 获取缺失属性

select * from customers
-- where phone is not null
where phone is null
select * from orders
where shipper_id is null and shipped_date is null

ORDER BY 子句

默认为升序,标签后跟 DESC 调整为降序

MySQL中可以用任何列排序数据,即使呢列没在 SELECT 子句中

select *,quantity* unit_price as 'total_price' from order_items
where order_id =2
-- order by quantity*unit_price desc
order by total_price desc

LIMIT 子句

limit n -- 取前n行
limit n,m -- n 为偏移量,m为偏移后取的行数
select * from customers
order by points desc
limit 3 -- 取第1,2,3行
limit 2,2 -- 取第3,4行

LIMIT 子句永远都要放在最后

总结:子句顺序

select
from
where
order by
limit

内连接 Inner Joins

连接多表列

select order_id,first_name,last_name,c.customer_id from orders o -- 设置表别名 o
inner join customers c -- inner 关键字可不写 -- 设置表别名 con o.customer_id = c.customer_id -- 添加链接条件 顾客id相同

跨数据库链接

select * from order_items as oi
join sql_inventory.products as pon oi.product_id = p.product_id

自连接

select e.employee_id,e.first_name,m.first_name as manager
from employees e
join employees mon e.reports_to = m.employee_id

连接多表

select o.order_id,o.order_date,c.first_name,c.last_name,os.`name` as 'status' from orders o
join customers con o.customer_id = c.customer_id
join order_statuses oson o.`status` = os.order_status_id
order by o.order_id
select p.date,p.invoice_id,p.amount,c.`name`,pm.`name` as 'pay method' from payments p
join clients con p.client_id = c.client_id
join payment_methods pmon p.payment_method = pm.payment_method_id

复合连接条件 (两列确定一条信息)

select * from order_items oi
join order_item_notes oinon oi.order_id = oin.order_Id and oi.product_id = oin.product_id

隐式连接语法 – 存在交叉连接隐患

-- 显式
-- select * from orders o
-- join customers c
--      on o.customer_id = c.customer_id
-- order by order_id-- 隐式
select * from orders o,customers c
where o.customer_id = c.customer_id -- 若取消 where 会造成交叉连接
order by order_id

外连接 Outer Joins – left join; right join

/*
select c.customer_id,c.first_name,o.order_id from customers c
-- left join orders o  -- 左连接-左表为主 customers 为左表,orders 为右表
right join orders o      -- 右连接-右表为主 on c.customer_id = o.customer_id
order by c.customer_id  */
/*
select c.customer_id,c.first_name,o.order_id from orders o
right join customers c  on c.customer_id = o.customer_id
order by c.customer_id */

select p.product_id,p.`name`,oi.quantity from products p
left join order_items oion p.product_id = oi.product_id
order by p.product_id

多表外连接

select c.customer_id,c.first_name,o.order_id,sh.`name`as shipper from customers c
left join orders oon c.customer_id = o.customer_id
left join shippers shon o.shipper_id = sh.shipper_id   -- 不使用外连接,只返回符合等号条件的数据
order by c.customer_id

select o.order_date,o.order_id,c.first_name,sh.`name` as shipper,os.`name` as `status` from orders o
join customers c -- 每笔订单都有顾客,可用内连接on c.customer_id = o.customer_id
left join order_statuses oson os.order_status_id = o.`status`
left join shippers shon sh.shipper_id = o.shipper_id
order by os.`name`

自外连接

select e.employee_id,e.first_name,m.first_name as manager from employees e
left join employees mon e.reports_to = m.employee_id

USING – 相同列名

select o.order_id,c.customer_id,sh.`name` as shipper from orders o
join customers c
--   on o.customer_id = c.customer_idusing (customer_id) -- 两张表有相同名的列,可用使用 using 简化语句
left join shippers shusing (shipper_id)

select * from order_items oi
join order_item_notes oin
--   on oi.order_id = oin.order_id and oi.product_id = oin.product_idusing (order_id,product_id)
select p.date,cl.`name` as client,p.amount,pm.`name` as payment_method from payments p
join clients clusing (client_id)
join payment_methods pmon p.payment_method = pm.payment_method_id

自然连接 natural Joins

交叉连接 cross Joins

-- 显式
/*
select sh.`name` as shipper,p.`name` as product from shippers sh
cross join products p
order by sh.`name` */
-- 隐式
select sh.`name` as shipper,p.`name` as product from shippers sh,products p
order by sh.`name`

联合 union

select customer_id,first_name,points,'Bronze' as type from customers
where points < 2000
union
select customer_id,first_name,points,'Silver' as type from customers
where points between 2000 and 3000
union
select customer_id,first_name,points,'Gold' as type from customers
where points > 3000
order by first_name


行操作

小问题:删除记录后自增id不连续

-- 每当删除记录后,需要新增之前,执行语句
alter table orders auto_increment = 1;
insert into orders (customer_id,order_date,`status`)
values(2,'2019-01-01',1);
select * from orders

添加单行

-- 单行
-- insert into customers
-- values (default,'John','Smith',default,default,'address','city','ca',default)
-- or
insert into customes (first_name,last_name,address,city,state)
values ('John','Smith','address','city','ca');
select * from customers

添加多行

-- 多行
insert into shippers(name)
values ('a'),('b'),('c');
select * from shippersinsert into products
values(default,'Jack',20,1.2),(default,'Jack',30,1.2),(default,'Jack',40,1.2);
select * from products;

分层行 ---- last_insert_id()

重点: 假如你使用单INSERT语句插入多个行, LAST_INSERT_ID() 只返回插入的第一行产生的值

alter table orders auto_increment = 1;
insert into orders(customer_id,order_date,status)
values(1,'1992-01-01',1);
insert into order_items
values(last_insert_id(),1,10,2.5),(last_insert_id(),2,20,3.5);

创建表复制 子查询

子查询是在另一段sql 语句中的选择语句

-- 创建新表
create table orders_archived as -- 订单存档 使用此方式mysql会忽略表格属性
-- insert into orders_archived -- 表存在直接插入即可
select * from orders  -- 子查询
where order_date < '2019-01-01'
create table invoices_archived as
select i.invoice_id,i.number,c.`name` as client,i.invoice_total,i.payment_total,i.invoice_date,i.payment_date,i.due_date
from invoices as i
join clients as cusing(client_id)
where payment_date is not NULL
order by i.invoice_id

更新单行 ---- update;set

update invoices
set payment_total=default,payment_date=default
where invoice_id=1; -- 更新 id为1 的记录
update invoices
set payment_total = invoice_total * 0.5, -- 结果存在问题 小数位为0 数据类型原因payment_date = due_date
where invoice_id = 3

更新多行

update invoices
set payment_date = default
where payment_total = 0 -- 对应多行数据
-- where 子句中所有的运算符也可以在这里
update customers
set points = points +50
where birth_date < '1990-01-01'

update 使用 子查询

子查询是在另一段sql 语句中的选择语句

update invoices
set payment_total = invoice_total * 0.5,payment_date = due_date
where client_id = (-- 返回客户名为Yadel的idselect client_id from clients where name = 'Yadel'
)
-- 该子查询返回多条记录不能使用 = 了 ,用 in 替代
-- where client_id in (select client_id from clients where state in ('CA','NY'))
update orders
set comments = 'gold customer'
where customer_id in (select customer_id from customerswhere points > 3000
)

删除行 — delete from

alter table shippers auto_increment = 1;
insert into shippers(name)
values('a'),('b');
delete from shippers
where name in ('a','b')

数据汇总查询

聚合函数

聚合函数只运行非空值

selectmax(invoice_total) as highest,min(invoice_total) as lowest,avg(invoice_total) as average,sum(invoice_total) as total, -- 统计所有记录sum(invoice_total*1.1) as change_total,count(invoice_total) as number_of_invoices,-- 排除重复条目count(distinct client_id) as no_double_records,-- distinct 去除重复项count(*) as total_records,-- 除了数值,也可用于日期,字符串max(invoice_date) as date_max,-- 聚合函数仅运行非空值count(payment_date) as number_of_paymentsfrom invoices
where invoice_date >= '2019-07-01'
select 'first half of 2019' as data_range,sum(invoice_total) as total_sales,sum(payment_total) as total_payment,sum(invoice_total - payment_total) as what_we_expect
from invoices
where invoice_datebetween '2019-01-01' and '2019-06-30'
union
select 'second half of 2019' as data_range,sum(invoice_total) as total_sales,sum(payment_total) as total_payment,sum(invoice_total - payment_total) as what_we_expect
from invoices
where invoice_date between '2019-06-30' and '2019-12-31'
union
select 'total' as data_range,sum(invoice_total) as total_sales,sum(payment_total) as total_payment,sum(invoice_total - payment_total) as what_we_expect
from invoices

数据分组(单列,多列)统计

单列

select client_id,sum(invoice_total) as total_sales
from invoices
-- 数据分类前添加筛选器
where invoice_date >= '2019-07-01'
-- 单列分组
group by client_id -- 对数据分组 根据client_id
-- 默认状态下数据是按照group by 子句中指定的列排序的
order by total_sales desc -- desc 降序


添加筛选器后:

多列

select state,city,sum(invoice_total) as total_sales
from invoices as i
join clients as c using(client_id)
group by state,city

select date,pm.`name` as payment_method,sum(p.amount) as total_payments -- 注意点 要用sum
from payments as p
join payment_methods as pm on pm.payment_method_id = p.payment_method
group by date,payment_method
order by date

分组统计前:

分组统计后:

having

having 在分组行之后筛选数据,数据特征一定要在select子句中,而where 不需要
where 在分组前筛选数据
select client_id,sum(invoice_total) as total_sales,count(*) as number_of_invoices
from invoices
-- 因为where在分组前,所有在此并不知道每个用户的销售额
-- where total_sales > 500
group by client_id
-- 使用having 子句在分组行后筛选数据
-- 筛选数据特征一定要是select 子句中存在的
having total_sales > 500 and number_of_invoices >5
-- 获取located在VA并且花费至少100的顾客
select c.customer_id, c.first_name,c.last_name,sum(oi.quantity*oi.unit_price) as total_sales
from customers as c
join orders as o using (customer_id)
join order_items as oi using (order_id)
where state = 'VA'
group by c.customer_id, c.first_name,c.last_name
having total_sales > 100

rollup 仅mysql存在

select state,city,sum(invoice_total) as total_sales
from invoices as i
join clients as c using (client_id)
-- rollup 运算符只能应用于聚合值sum的列
-- 当你进行多列分组并运用rollup 运算符,可以看到每个组以及整个结果集的汇总值
group by state,city with rollup

select pm.`name`,sum(p.amount) as total
from payments as p
join payment_methods as pmon p.payment_method = pm.payment_method_id-- 在使用rollup 运算符时不能在group by 子句中使用列别名
group by pm.`name` with rollup


使用别名会显示异常:

传送门

SQL进阶教程 | 史上最易懂SQL教程!10小时零基础成长SQL大师!!
mysql last_Insert_id()用法
mysql id从1开始自增 解决id不连续的问题

Mosh--mysql 学习笔记(1)相关推荐

  1. 【mysql学习笔记整理】

    /*mysql学习笔记整理*/ /*常用的数据库操作对象*/ #库的操作 #创建 #数据库的创建 USE mysql; CREATE DATABASE db_x; #删除 #删除数据库 DROP DA ...

  2. 初识mysql学习笔记

    使用VMVirtualBox导入Ubuntu后,可以通过sudo apt-get install mysql-server命令下载mysql. 在学习过程中,我遇到了连接不上Xshell的问题.最终在 ...

  3. MySQL学习笔记07【事务、用户管理和权限管理】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  4. MySQL学习笔记06【多表查询、子查询、多表查询练习】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  5. MySQL学习笔记05【多表操作、三大范式、数据库的备份和还原】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  6. MySQL学习笔记04【数据库的查询操作、今日内容、表的约束】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  7. MySQL学习笔记03【数据库表的CRUD操作、数据库表中记录的基本操作、客户端图形化界面工具SQLyog】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  8. MySQL学习笔记02【SQL基本概念与通用语法、数据库的CRUD操作】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  9. MySQL学习笔记01【数据库概念、MySQL安装与使用】

    MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...

  10. Mysql学习笔记(七)查(补充)

    Mysql学习笔记(七)查(补充) 原文:Mysql学习笔记(七)查(补充) PS:五一还是要学习...虽然有点苦逼..但是路是自己选的,那么自己就要坚持的走下去... 学习内容: 1.数据库查找的补 ...

最新文章

  1. Jquery 选择器大全 【转载】
  2. C# OOP 重要部分全解
  3. 文件写入的6种方法,这种方法性能最好
  4. 面试:对象的生命周期
  5. 硬核!有人开源了一套呼吸机方案!
  6. springBoot跨域解决
  7. python and or优先级_python的and和or优先级
  8. 高通driver模块编译方法
  9. 你真的了解开源镜像站吗?
  10. 网络抖动多少ms算正常_如何测试延时、抖动、丢包率
  11. JVM常见面试题汇总笔记
  12. eclipse 缓解眼睛疲劳保护眼睛
  13. GIS应用技巧之密度分析
  14. 同心向前,Google Play 十周年啦!
  15. 2.07 CISC与RISC
  16. 2022年餐饮行业的10大必看趋势
  17. github官网老是打不开
  18. Demo : 人脸5个关键点检测
  19. 练习题(困难) 百慕大三角 POJ1069
  20. 前端实现录音功能 语音录入 弹框录入

热门文章

  1. 【Gson】【1】Gson使用简介
  2. OpenSuse11.4+Apache+Django搭建Webserver
  3. Entity Framework 实践系列 —— 搞好关系 - 单相思(单向一对一,one-to-one)
  4. CSDN在2017年度的若干“升级”
  5. 第四章 网络层[练习题+课后习题]
  6. PTA: 6-6 链表拼接(20分)
  7. 《Python cookbook》笔记二
  8. linux下怎么解压tar.xz,Linux下解压.tar.xz格式文件的方法
  9. js和php难度,有js基础和简单的php基础,但是学习nodejs还是很吃力
  10. java怎么对用户做自定义模版打印_Printing tools 自定义模板打印的实现