SELECT order_id, o.customer_id, first_name, last_name
FROM orders o
-- 可以给常用的名字进行缩写JOIN customers c ON o.customer_id = c.customer_id
-- 根据customer_id将两个表合并在一起
-- 小小练习SELECT order_id, o.product_id, quantity, o.unit_price
FROM order_items o
JOIN products p ON p.product_id = o.product_id
跨数据库连接
给不在当前数据库的表加前缀
SELECT*FROM order_items oi
-- 给不在当前查询库的表加上他自己的库前缀JOIN sql_inventory.products p ON oi.product_id = p.product_id
自连接
同一张表需要使用不同的别名,查询的每列前要指定别名
SELECT e.employee_id, e.first_name, m.first_name AS manager
-- 别名eFROM employees e
-- 别名mJOIN employees m ON e.reports_to = m.employee_id
多表连接
一直往下写JOIN ON就行
SELECT o.order_id, o.order_date, c.first_name, c.last_name, os.name AS states
-- 多表连接 接着往下写JOIN ON就行FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN order_statuses os ON o.status= os.order_status_id
-- 小小练习SELECT p.date, p.invoice_id, p.amount,
c.name, pm.name
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
复合连接条件
使用多个条件连接表格
JOIN ON 1 AND 2
SELECT*FROM order_items oi
JOIN order_item_notes oin ON oi.order_id = oin.order_Id
-- 用AND连接更多的条件AND oi.product_id = oin.product_id
隐式连接语法
不推荐使用隐式连接语法,尽量使用显示语法JOIN ON
-- 以下两种查询结果是一样的-- 尽量使用显示语法SELECT*FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
-- 隐式连接语法,但不推荐使用-- 使用隐式连接语法时,如果忘记写WHERE了,就会得到交叉连接的结果【笛卡尔积】SELECT*FROM orders o, customers c
WHERE o.customer_id = c.customer_id
外连接
LEFT JOIN
RIGHT JOIN
外连接:两个表在连接过程中除了返回满足连接条件的行以外,还返回左表或右表不满足连接条件的行
-- 内连接SELECT c.customer_id, c.first_name, o.order_id
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
ORDERBY c.customer_id;-- 外连接:LEFT JOIN / RIGHT JOINSELECT c.customer_id, c.first_name, o.order_id
FROM customers c
-- 左表中的顾客会被全部返回LEFTJOIN orders o ON c.customer_id = o.customer_id
ORDERBY c.customer_id;-- 外连接:LEFT JOIN / RIGHT JOINSELECT c.customer_id, c.first_name, o.order_id
FROM customers c
-- 右连接的结果和内连接一样。RIGHTJOIN orders o ON c.customer_id = o.customer_id
ORDERBY c.customer_id;
-- 小小练习SELECT p.product_id, name, quantity
FROM products p
LEFTJOIN order_items oi ON p.product_id = oi.product_id;
多表外连接
-- 多表外连接SELECT c.customer_id, c.first_name, o.order_id, sh.name AS shipper
FROM customers c
-- o表和c表连,sh表和o表连LEFTJOIN orders o ON c.customer_id = o.customer_id
LEFTJOIN shippers sh ON o.shipper_id = sh.shipper_id
ORDERBY c.customer_id;
-- 小小练习SELECT o.order_date, o.order_id, c.first_name, sh.name, os.name
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
LEFTJOIN order_statuses os ON o.status= os.order_status_id
LEFTJOIN shippers sh ON o.shipper_id = sh.shipper_id
ORDERBYstatus
自外连接
一个表用外连接来连接自己
USE sql_hr;SELECT e.employee_id, e.first_name, manager.first_name AS manager
FROM employees e
LEFTJOIN employees manager ON e.reports_to = manager.employee_id
USING子句
使用USING可以是JOIN ON后的条件更简介
USING (列名)
USING关键字只能在不同表中的列名字完全一样的情况下使用
USE sql_store;SELECT o.order_id, c.first_name, sh.name AS shipper
FROM orders o
JOIN customers c USING(customer_id)LEFTJOIN shippers sh USING(shipper_id)
-- 使用JOIN ON 写法较为复杂SELECT*FROM order_items o
JOIN order_item_notes oin
ON o.order_id = oin.order_Id AND
o.product_id = oin.product_id;-- 同样的查询,使用USING,写法简介SELECT*FROM order_items o
JOIN order_item_notes oin
USING(order_id, product_id);
-- 小小练习USE sql_invoicing;SELECT
p.date,
c.name AS client,
p.amount,
pm.name AS name
FROM payments p
JOIN clients c USING(client_id)-- 两个表中的列名不一致,不能用USING,只能用ON查询JOIN payment_methods pm ON p.payment_method = pm.payment_method_id;
自然连接
写起来简单,但不建议使用。数据库引擎会自己看着办去连接两个表
USE sql_store;SELECT*FROM orders
-- 此时数据库引擎会自己看着办,基于两个表中共同的列连接NATURALJOIN customers c
交叉连接
和前面的隐式连接语法一样,得到笛卡尔积的结果
交叉连接有隐式语法和显示语法
显示语法:CROSS JOIN
隐式语法,即隐式连接语法,FROM 表1, 表2
-- 显示语法SELECT c.first_name AS customer,
p.name AS product
FROM customers c
CROSSJOIN products p
ORDERBY c.first_name;-- 隐式语法SELECT c.first_name AS customer,
p.name AS product
FROM customers c, products p
ORDERBY c.first_name;