https://www.w3school.com.cn/sql/sql_datatypes.asp
暂停执行:--(注释)
加一行新列并命名:
SELECT (points+10) * 100 AS 'discount factor'
删除重复项:
SELECT DISTINCT
不等于:!= 或者 <>
等于条件一个等号 WHERE state = 'va'
日期也要加上单引号 WHERE birthdate > '1990-01-01'
多个条件:
WHERE birthdate > '1990-01-01' AND points > 1000
WHERE birthdate > '1990-01-01' OR points > 1000
AND运算符优先于OR运算符
IN运算符
WHERE state IN ('VA' , 'CA', 'WS')
WHERE quantity IN (49, 39, 23)
BETWEEN运算符
WHERE points BETWEEN 100 AND 300
(两边是等于)
LIKE运算符
WHERE last_name LIKE 'b%'
(查询以B为开头的)
WHERE last_name LIKE 'b____y'
WHERE number NOT LIKE '%9'
(查询尾号不是9的)
REGEXP
WHERE last_name REGEXP '^filed'
(以field为开头)
WHERE last_name REGEXP 'filed$'
(以field为结尾)
WHERE last_name REGEXP 'filed|mac'
(含有field或mac,三个也可以)
WHERE last_name REGEXP '[gim]e'
(含有ge或ie或me都可以)
WHERE last_name REGEXP '[a-h]e'
(含a-h任意字母与e)
IS NULL
WHERE phone IS NULL
WHERE phone IS NOT NULL
ORDER BY
如果没有,就按照主列排序(如id)
ORDER BY first_name DESC
DESC表示降序排列
ORDER BY state, first_name
(按洲排序,相同的洲按名字排序)
LIMIT
LIMIT 3 只显示前三行
LIMIT 6, 3(6为偏移量,跳过6个再选后面三个)
连接
INNER JOIN内连接
INNER可以不写
FROM orders
JOIN customers
ON orders.customer_id = customer.customer_id
(将customer文件中customer_id与order文件中相等的那一部分,加入order文件中)
【根据ID将两个表合并】
定义简写:
FROM order_items oi
JOIN products p ON
(如果给了表一个别名,所有其他地方也一定要用这个别名)
跨数据库连接
SELECT *
FROM order_items oi
JOIN sql_inventory.product p
ON oi.product_id = p.product_id
自连接
SELECT *
FROM employees e
JOIN employees m
ON e.reports_to = m.emplyee_id
(用两个不同的简写名称给同一个表)
SELECT
e.employee_id,
e.first_name,
m.first_name AS manager
FROM employees e
JOIN employees m
隐式连接语法
(以下两种方式结果相同,下面一种为隐式连接语法)
SELECT *
FROM orders o
JOIN customer c
ON o.customer_id = c.customer_id
----Implpicit Join Syntax
SELECT *
FROM order o, customer c
WHERE o.customer_id = c.customer_id
(如果没有WHERE那一行就会交叉连接)
左连接
SELECT
c.customer_id,
c.first_name,
o.order_id
FROM customers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
ORDER BY c.customer_id
(xianshi左表即customers里的全部内容,不管条件正确还是错误)
右连接:右表全部返回
多表外连接
SELECT
c.customer_id,
c.first_name,
o.order_id
sh.name AS shipper
FROM custimers c
LEFT JOIN orders o
ON c.customer_id = o.customer_id
LEFT JOIN shippers sh
ON o.shipper_id = sh.shipper_id
ORDER BY c.customer_id
USING子句(替换ON语句)
SELECT
o.order_id,
c.first_name
FROM orders o
JOIN customers c
USING (customer_id)
JOIN shippers sh
USING (shipper_id)
USING关键词只能在不同表中额列名字完全一样的情况下使用
自然连接
基于共同列连接
SELECT
order_id,
first name
FROM order o
NATURAL JOIN customers c
交叉连接
隐式语法
SELECT
sh.name AS shipper,
p.name AS product
FROM shippers sh, product
ORDER BY sh.name
显式语法
SELECT
sh.name AS shipper,
p.name AS product
FROM shippers sh
CROSS JOIN products p
ORDER BY sh.name
联合
SELECT
order_id,
order_data,
'Active' AS status #改名
FROM orders
WHERE order_date >= '2019-01-01'
UNION #合并上下查询的记录
SELECT
order_id,
order_data,
'Actived' AS status #改名
FROM orders
WHERE order_date < '2019-01-01'
SELECT first_name
FROM customers
UNION
SELECT name
FROM shipper
(显示一列名字,是customers表和shippers表的联合)
(查询返回的列的数量一定要相等)
如果要把联合之后的列名修改
SELECT first_name AS full name
练习:根据不同积分算等级(青铜白银黄金)
SELECT customer_id, first_name, points, 'Bronze'
FROM customers
WHERE points < 2000
UNION
SELECT customer_id, first_name, points, 'Sliver'
FROM customers
WHERE points BETWEEN 2000 AND 3000
UNION
……
ORDER BY first_name