第一章 SELECT语句
### SELECT 子句
```sql
语法:
---新+行重新命名为:discount_factor
SELECT
first_name,
last_name,
points,
(points + 10) * 100 AS discount_factor
FROM customers
练习:
---查询价格,新增价格:原价*1.1
SELECT
name,
unit_price,
unit_price * 1.1 AS 'new price'
FROM products
```
### WHERE子句
```sql
语法:
---选择积分大于3000的用户
SELECT *
FROM customers
WHERE points > 3000
---sql标准日期写法,选择出生日期>1990-01-01的人
SELECT *
FROM customers
WHERE birth_date > '1990-01-01'
练习:
---查询>2019-01-01的订单
SELECT *
FROM orders
WHERE order_date >= '2019-01-01'
```
### AND,OR,NOT运算符
```sql
运算符基础语法:
---查询生日>1990-01-01或者point>1000积分的
SELECT *
FROM customers
WHERE birth_date > '1990-01-01' AND points > 1000
AND练习:
---查询id=6的,库存*价格>30的表
SELECT *
FROM order_items
WHERE order_id = 6 AND (quantity * unit_price) > 30
IN运算符语法:
---查询同时等于VA,FL,GA,的数据
SELECT *
FROM customers
WHERE state IN ('va', 'fl', 'ga')
IN运算符练习:
---查询stock中等于49,38,72的数据
SELECT *
FROM products
WHERE quantity_in_stock IN (49, 38, 72)
BETWEEN运算符语法:
---查询积分在1000到3000之间的数据
SELECT *
FROM customers
WHERE points BETWEEN 1000 AND 3000
BETWEEN运算符练习:
---查询出生日在1990到2000之间的数据
SELECT *
FROM customers
WHERE birth_date BETWEEN '1990-01-01' AND '2000-01-01'
LIKE运算符语法:
---查询以b开头为名字的顾客数据,%代表任意匹配
SELECT *
FROM customers
WHERE last_name LIKE 'b%'
REGEXP(正则表达式)运算符语法:
---‘|’管道符或连接;【a-h】a-h范围匹配;‘^’匹配前面任意;'$'匹配任意后面
SELECT *
FROM customers
WHERE last_name REGEXP 'field|mac'
WHERE last_name REGEXP '[gim]e'
练习:
---查询包含AMBUR和ELKA的数据
SELECT *
FROM customers
WHERE first_name REGEXP 'AMBUR|ELKA'
SELECT *
FROM customers
WHERE last_name REGEXP 'EY$|ON$'
SELECT *
FROM customers
WHERE last_name REGEXP '^my|se'
SELECT *
FROM customers
WHERE last_name REGEXP 'b[ru]'
NULL运算符:
---查询为空的数据
SELECT *
FROM customers
WHERE phone IS NULL
---查询没有发货的数据,实际经常使用的语句
SELECT *
FROM orders
WHERE shipped_date IS NULL
```
### ORDER BY子句
```sql
ORDER BY语法:
---排序
SELECT *
FROM customers
ORDER BY first_name
LIMIT语法:
---先排序再取前三数据
SELECT *
FROM customers
ORDER by points DESC
LIMIT 3
```
# 第二章 joins(连接)
### 1.内链接
```sql
内链接语法:
---内链查询客户表和订单表
SELECT order_id, o.customer_id, first_name, last_name
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
---查询内连接订单和产品
SELECT order_id, o.product_id, quantity, o.unit_price
FROM products p
JOIN order_items o
ON p.product_id = o.product_id
跨数据库连接语法:
---sql_store连接到sql_inventory数据库
SELECT *
FROM order_items oi
JOIN sql_inventory.products p
ON oi.product_id = p.product_id
自连接语法:(多用于组织架构图)
---自连接员工与管理员
SELECT *
FROM employees e
JOIN employees m
ON 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 c
ON o.customer_id = c.customer_id
JOIN order_statuses os
ON o.status = os.order_status_id
多key表连接语法:
---多key表连接语法
SELECT *
FROM order_items oi
JOIN order_item_notes oin
ON oi.order_id = oin.order_id
AND oi.product_id = oin.product_id
隐士连接语法:
---
```
### 2.外连接
```sql
外链接语法:
---外连接,查询包含订单为null的所有数据(内链接不包含null数据)
SELECT
c.customer_id,
c.first_name,
o.order_id
FROM orders o
RIGHT JOIN customers c
ON c.customer_id = o.customer_id
外连接练习:
---外链接查询
SELECT
oi.product_id,
p.name,
oi.quantity
FROM order_items oi
RIGHT JOIN products p
ON oi.product_id = p.product_id
多表外链接:
---
SELECT *
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
LEFT JOIN shippers sh
ON o.shipper_id = sh.shipper_id
多表外链接lianxi:
---3
SELECT
o.order_id,
o.order_date,
c.first_name AS customer,
sh.name AS shipper
FROM orders o
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
```
### USING子句
```sql
USING语法:
---连接的N张表中相同的名字可以用USING()来表示
SELECT *
FROM orders o
JOIN customers c
-- ON o.customer_id = customers.customer_id
USING(customer_id)
!!!自然连接语法:不建议使用,数据库自动连接,无法控制
SELECT
*
FROM orders o
NATURAL JOIN customers c
交叉连接:
---
SELECT
c.customer_id,
c.first_name,
p.name
FROM customers c
CROSS JOIN products p
联合查询| UNION:
---
SELECT
o.order_id,
o.order_date,
'Active' AS status
FROM orders o
WHERE o.order_date >= '2019-01-01'
UNION
SELECT
o.order_id,
o.order_date,
'Archived' AS status
FROM orders o
WHERE o.order_date < '2019-01-01'
UNION练习:
---根据orders表积分划分会员级别:青铜-白银-黄金
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
```
# 第三章 增删改
```sql
插入行语法:
---
INSERT INTO customers---(可以加入需要插入的值)
VALUES (
DEFAULT,
'john',
'smith',
'1990-1-1',
NULL,
'address',
'city',
'ca',
0)
插入多行语法:
---增加多行
INSERT INTO shippers (name)
VALUES ('shipper1'),
('shipper2')
单次插入多行练习:
---在产品表中一次增加3行,包括(名称,价格,数量)的数据
INSERT INTO products( name, quantity_in_stock, unit_price)
VALUES
('baicai', 5, 3),
('doufu', 20, 3),
('rou', 15, 3)
插入分层行:
---母子关联行插入(orders关联orders_team)
INSERT INTO orders (customer_id, order_date, status)
VALUES (1, '2019-01-02', 1);
INSERT INTO order_items
VALUES
(LAST_INSERT_ID(), 1, 2, 2.95),
(LAST_INSERT_ID(), 2, 2, 4.95)
创建表复制语法:
---格式+各种查询子句任意组合
CREATE TABLE orders_archived AS---格式
SELECT * FROM orders---随意写各种查询子句
更改表数据:
---
UPDATE invoices---选择表
SET payment_total = 0, payment_date = NULL---更改内容
WHERE invoice_id = 1---选择行
练习:
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE invoice_id = 3
更改多行:
---
UPDATE invoices
SET
payment_total = invoice_total * 0.5,
payment_date = due_date
WHERE client_id = 3---选择所有id为3的全部更改
WHERE client_id IN(3,4)---选择所有id为3和4的全部更改
练习:
---查询所有大于90年的积分+50
UPDATE customers
SET
points = points + 50
WHERE
birth_date < '1990-01-01'
更新表中使用子查询语法:
---先用SELECT子查询找到积分>3000的,然后在更新为Gode
UPDATE orders
SET comments = 'Goad'
WHERE customer_id IN
(SELECT customer_id
FROM customers
WHERE points > 3000 )
删除行:
--- 使用子查询删除某行
DELETE FROM invoices
WHERE client_id =(
SELECT client_id
FROM clients
WHERE name = 'Myworks'
)