stringcc

mySQL第三章

01-内连接

image
image
image

点击查看代码
SELECT  oi.order_id, oi.product_id, p.name, oi.quantity, oi.unit_price 
FROM order_items oi 
JOIN products p  
	ON oi.product_id = p.product_id

image

02 跨数据库连接

image

点击查看代码
SELECT *
FROM sql_store.order_items oi
JOIN sql_inventory.products p
		ON oi.product_id = p.product_id

image

03 自连接

image

点击查看代码
USE sql_hr;
SELECT
	e.employee_id,
	e.first_name AS employee,
	m.first_name AS manager
 FROM employees e
 JOIN employees m
	ON e.reports_to = m.employee_id
reports_to(向谁汇报,谁是上级,上级也是公司的雇员

image

04 多表连接

image
image
image
image

点击查看代码
SELECT 
	p.date,
	p.invoice_id,
    p.amount,
    c.name,
    pm.name AS 'payments method'
FROM payments p
JOIN clients c
	ON p.client_id = c.client_id
JOIN payment_methods pm
	ON p.payment_method = pm.payment_method_id

image

05 复合连接

订单id和产品id在一起才能唯一表示一条记录,所以叫复合主键

image

要记得改数据
原来是

image

要改成

image

点击查看代码
SELECT *
FROM order_item_notes oin
JOIN order_items oi
	ON oin.order_Id = oi.order_id
    AND oin.product_id = oi.product_id

image

06 隐式连接语法 Implicit Join Syntax

两个语法等价,但最好用显式
如果忘记写 where 就会发生交叉连接

点击查看代码
SELECT *
FROM orders o
JOIN  customers c
	ON o.customer_id = c.customer_id;
    
SELECT *
FROM orders o , customers c
WHERE o.customer_id = c.customer_id
    

image

07 外连接 OUTER JOIN

直接JOIN 就表示 inner join 写 left 和right 就表示 outer join 左右连接只是表示基于哪张表而已

目标显示下列信息

image

需要两张表

image

要看到每个产品被订购了多少次 (条件)
但是有些产品没有被订购,也需要显示, 就是所有产品都要显示

不管条件是否成立,都会显示左表或者右表的数据

点击查看代码
SELECT 
	p.product_id,
    p.name,
    oi.quantity,
    oi.order_id
FROM  order_items oi
RIGHT JOIN products p
	ON oi.product_id = p.product_id

image

08 多表外连接

就是超过两个表的连接

最好不要同时使用左连接和右连接,不易读懂(容易被后面看你代码的人骂)
尽量使用左连接
excise

image

以oders表为为主表,和 customers,shippers,order_statuses连接
不管有没有发货,都必须要显示订单信息 ,left 左连接

点击查看代码
SELECT
    o.order_id,
    o.order_date,
    c.first_name AS customer,
    sh.name AS shipper,
    os.name  AS status
FROM orders o
JOIN customers c
	ON o.customer_id = c.customer_id
LEFT JOIN  shippers  sh
	ON o.shipper_id = sh.shipper_id
JOIN order_statuses os
	ON o.status = os.order_status_id
ORDER BY status , order_id
    

image

09 自外连接

不管是否有上级,都要显示所有的员工和上级情况

image

点击查看代码
USE sql_hr;
SELECT 
	e.employee_id,
    e.first_name AS employee,
    m.first_name AS manager
FROM  employees e
LEFT JOIN employees m
	ON e.reports_to = m.employee_id
    

10 USING 子句,可替换 JOIN后面的ON 语句 更短更易读

仅限于两个表的列名一样
复合连接中 可以 替换 and 后面的

exercise

image

回到 发票数据库

image

点击查看代码
USE sql_invoicing;
SELECT 
	p.date,
    c.name AS client,
    p.amount,
    pm.name AS 'payment method'
FROM  payments p	
JOIN clients c USING (client_id)
-- 	ON p.client_id =c.client_id
JOIN payment_methods pm
	ON p.payment_method = pm.payment_method_id

	
    

image

11 自然连接(佛系连接,系统看着办)不建议使用

使用 natural 数据库会自己找相同列连接(靠猜)

试试刚刚的例子

image

自动找到 client_id 作为连接

12 交叉连接 = 笛卡尔积

就是左表的每一行和右表每一行 互相排列组合
一张表的行数乘以另一张表的行数

image

image

image

image

image

image

image

显示语法

image

隐式语法 ,不使用where, 直接from 后面加两个表

image

exercise

image

显示所有发货商名字和产品的组合

点击查看代码
USE sql_store;
SELECT 
	sh.name AS shipper,
	p.name AS product
FROM products p, shippers sh
-- CROSS JOIN shippers sh
ORDER BY sh.name

image

13 UNION 显示多张表的行 功能强大

image

image

exercise

image

顾客表中没有 type 列所以要根据积分计算
<2000 青铜 [2000,3000) 白银 >= 3000 黄金

点击查看代码
USE sql_store;
SELECT 
	c.customer_id,
    c.first_name,
    c.points,
    'Bronze' AS type
 FROM customers c
 WHERE points < 2000
 
 UNION 
 
 SELECT 
	c.customer_id,
    c.first_name,
    c.points,
    'Silver' AS type
 FROM customers c
 WHERE points >= 2000 and points < 3000

UNION

 SELECT 
	c.customer_id,
    c.first_name,
    c.points,
    'Gold' AS type
 FROM customers c
 WHERE points >= 3000 
 ORDER BY first_name
 

image

以上是在 学习连接

这里的个人学习记录和一些笔记

posted on 2026-11-08 11:08  晓晓言  阅读(7)  评论(0)    收藏  举报

导航