notebook_sql
这是我自己学习SQL的笔记,教程是来自B站的视频,主讲人是在Youtube非常火的MOSH
课程链接
1.1 What is SQL
A DATABASE is a collection of data stored in a format that can easily be accessed.
1.2 TYPE OF DATABASE
Relational database & No-SQL
Relational Database Management System: MySQL, SQL Server, Oracle
No-SQL: no relationship between tables
NoSQL system don’t understand SQL
2.1 Create the databases
-- USE sql_store; 使用哪个数据库
SELECT * FROM customers
WHERE customer_id =1
ORDER BY first_name;
2.2 THE SELECT Statement
SELECTlast_name,first_name,points,(points + 10) * 100 AS discount_factor-- 遵循数学表达式的优先性-- AS "discount facotr" 如果字母间有空格必须加上""
FROM customers;
2.3 THE SELECT CLAUSE
SELECT DISTINCT state -- DISTINCT 表示在state中不显示重复的
FROM customers;
EXERCISE
SELECT name,unit_price,unit_price * 1.1 AS new_price
FROM customers;
2.4 THE WHERE CLAUSE
SELECT *
FROM customers
WHERE birth_date > '1990-01-01';-- state!= 'VA'-- points > 3000-- </>=/<=/=/!=
EXERCISE
-- 查看今年的订单
SELECT *
FROM orders
WHERE order_date >= '2019-01-01';
2.5 THE AND/OR/NOT
SELECT *
FROM customers
-- WHERE birth_date > '1990-01-01' OR (points > 1000 AND state = 'VA')-- OR points > 1000
WHERE NOT (birth_date > '1990-01-01' OR points > 1000);
EXERCISE
-- exercise
SELECT *
FROM order_items
WHERE order_id = 6 AND (unit_price * quantity) >30;
2.6 THE IN OPERATOR
SELECT *
FROM customers-- 在SQL语言中我们不能将字符穿和布尔表达式进行结合 -- OR 'GA' OR 'FL'
WHERE state = 'VA' OR state = 'GA' OR state = 'FL';
-- WHERE state NOT IN ('VA','FL','GA')
EXERCISE
-- EXERCISE
SELECT *
FROM products
WHERE quantity_in_stock IN (49,38,72);
2.7 THE BETWEEN OPERATOR
SELECT *
FROM customers
-- WHERE points>= 1000 AND points<= 3000
-- 用between语法来表示
WHERE points BETWEEN 1000 AND 3000;
EXERCISE
SELECT *
FROM customers
WHERE birth_date BETWEEN '1990-01-01'AND '2000-01-01';
2.8 THE LIKE OPERATOR
SELECT *
FROM customers
WHERE last_name LIKE -- "%b%" -- %来表示任意字符-- 名字中有"_____y" -- 名字中第_____个字符是y
EXERCISE
-- exercise address匹配
SELECT *
FROM customers
WHERE address LIKE '%TRAIL%' -- BTWEEN IN 是精准定位而like表示模糊定位OR address LIKE '%avenue%'
-- EXERCISE
SELECT *
FROM customers
WHERE phone LIKE '%9';
2.9 THE REGEXP OPERATOR
SELECT *
FROM customers
WHERE last_name LIKE '%field%';
SELECT *
FROM customers
WHERE last_name REGEXP 'field$|mac|rose';
– ^表示在字符串开头
– $ 表示在字符串结尾
– | 可以同时匹配两个字符串,是或的意思
SELECT *
FROM customers
WHERE last_name REGEXP '[gim]e';
Output: ge/ie/me
SELECT *
FROM customers
WHERE last_name REGEXP '[a-h]e';
Output: ae/be/…/he
EXERCISE
FIRSTNAME中带ELKA或AMBUR
-- EXERCISE REGEXP1
SELECT *
FROM customers
WHERE FIRST_name REGEXP 'ELKA|AMBUR';
LASTNAME中以EY或ON结尾
-- EXERCISE REGEXP2
SELECT *
FROM customers
WHERE last_name REGEXP 'EY$|ON$';
lastname中以my开头并且里面带se
-- EXERCISE REGEXP3
SELECT *
FROM customers
WHERE last_name REGEXP '^MY|SE';
名字中带BR或BU
-- EXERCISE REGEXP4
SELECT *
FROM customers
WHERE last_name REGEXP 'B[RU]';
正则表达式
模式 | 描述 |
---|---|
^ | 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 ‘\n’ 或 ‘\r’ 之后的位置。 |
$ | 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 ‘\n’ 或 ‘\r’ 之前的位置。 |
. | 匹配除 “\n” 之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符,请使用像 ‘[.\n]’ 的模式。 |
[…] | 字符集合。匹配所包含的任意一个字符。例如, ‘[abc]’ 可以匹配 “plain” 中的 ‘a’。 |
[^…] | 负值字符集合。匹配未包含的任意字符。例如, ‘[^abc]’ 可以匹配 “plain” 中的’p’。 |
p1|p2|p3 | 匹配 p1 或 p2 或 p3。例如,‘z|food’ 能匹配 “z” 或 “food”。’(z|f)ood’ 则匹配 “zood” 或 “food”。 |
* | 匹配前面的子表达式零次或多次。例如,zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。 |
+ | 匹配前面的子表达式一次或多次。例如,‘zo+’ 能匹配 “zo” 以及 “zoo”,但不能匹配 “z”。+ 等价于 {1,}。 |
{n} | n 是一个非负整数。匹配确定的 n 次。例如,‘o{2}’ 不能匹配 “Bob” 中的 ‘o’,但是能匹配 “food” 中的两个 o。 |
{n,m} | m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。 |
\ | 将下一个字符标记为或特殊字符、或原义字符、或向后引用、或八进制转义符。例如, ‘n’ 匹配字符 ‘n’。’\n’ 匹配换行符。序列 ‘\’ 匹配 “”,而 ‘(’ 则匹配 “(”。 |
2.10 THE IS NULL OPERATOR
SELECT *
FROM customers
WHERE phone IS NOT NULL;
EXERCISE
-- EXERCISE
SELECT *
FROM orders
WHERE shipped_date IS NULL;
2.11 THE ORDER BY CLAUSE
MYSQL中可以用任何列排列数据,不管那列是不是在select子句中
SELECT first_name, last_name
FROM customers
ORDER BY birth_date;
排序可以根据自己起的列排序
SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY points, first_name;
1–represents first_name
2–represents last_name
最好避免使用
SELECT first_name, last_name, 10 AS points
FROM customers
ORDER BY 1,2;
exercise
order_id=2 并且根据每一项目的总价格进行排序
SELECT *
FROM order_items
WHERE order_id =2
ORDER BY quantity * unit_price DESC;
SELECT order_id, product_id, quantity, unit_price,(quantity * unit_price) AS Total_price
FROM order_items
WHERE order_id =2
ORDER BY Total_price DESC;
2.12 THE LIMIT CLAUSE
SELECT *
FROM customers
LIMIT 3
OFFSET
– page 1: 1-3
–page 2: 4-6
–page 3: 7-9
如果想直接获取第三页的数据,需要跳过前面的6条记录并选择3条记录
SELECT *
FROM customers
LIMIT 6,3;
LIMIT 6,3 --跳过前面的6条记录,并选择6条记录后的3条记录
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MWpalSSO-1610167958984)(C:\Users\Changnie\AppData\Roaming\Typora\typora-user-images\image-20201213173050617.png)]
EXERCISE
查找积分最多的前三名用户
-- Get the top three loyal customers
SELECT *
FROM customers
ORDER BY points DESC
LIMIT 3;
3.1 INNER JOINS
SELECT *
FROM orders
INNER JOIN customers ON customers.customer_id = orders.customer_id;
SELECT order_id, first_name, last_name
FROM orders
INNER JOIN customers ON customers.customer_id = orders.customer_id;
Error Code: 1052. Column ‘customer_id’ in field list is ambiguous 0.000 sec
SELECT order_id, customer_id, first_name, last_name
FROM orders
INNER JOIN customers ON customers.customer_id = orders.customer_id;
Reason: orders & customers表格都有customer_id 这个列,MySQL无法确定我们想从哪张表选取这列
How to solve it?
SELECT order_id, orders.customer_id, first_name, last_name
FROM orders
INNER JOIN customers ON customers.customer_id = orders.customer_id;
在customer_id前面标注是哪个表中的columns
用o代替orders,用c代替customers
SELECT order_id, o.customer_id, first_name, last_name
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id;
EXERCISE
SELECT order_id,p.product_id,name,quantity, o.unit_price
FROM order_items o
JOIN products pON o.product_id = p.product_id;
Output
3.2 Joining Across Databases
将分散在多个数据库的表中的列合并起来
尝试将sql_inventory中的products与sql_store order_items链接到一起
SELECT *
FROM order_items o
JOIN sql_inventory.products pON o.product_id = p.product_id;
因为是USE sql_store所以sql_inventory.products p
Another Situation(USE sql_inventory)
USE sql_inventory;
SELECT *
FROM products p
JOIN sql_store.order_items oiON p.product_id= oi.product_id;
3.3 Self Joins
使表自己和自己进行连结,查看每个员工和他的manager
USE sql_hr;SELECT *
FROM employees e
JOIN employees m -- m for managerON e.reports_to = m.employee_id;
Output
employee_id | first_name | last_name | job_title | salary | reports_to | office_id | employee_id | first_name | last_name | job_title | salary | reports_to | office_id |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
33391 | D’arcy | Nortunen | Account Executive | 62871 | 37270 | 1 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
37851 | Sayer | Matterson | Statistician III | 98926 | 37270 | 1 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
40448 | Mindy | Crissil | Staff Scientist | 94860 | 37270 | 1 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
56274 | Keriann | Alloisi | VP Marketing | 110150 | 37270 | 1 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
63196 | Alaster | Scutchin | Assistant Professor | 32179 | 37270 | 2 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
67009 | North | de Clerc | VP Product Management | 114257 | 37270 | 2 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
67370 | Elladine | Rising | Social Worker | 96767 | 37270 | 2 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
68249 | Nisse | Voysey | Financial Advisor | 52832 | 37270 | 2 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
72540 | Guthrey | Iacopetti | Office Assistant I | 117690 | 37270 | 3 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
72913 | Kass | Hefferan | Computer Systems Analyst IV | 96401 | 37270 | 3 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
75900 | Virge | Goodrum | Information Systems Manager | 54578 | 37270 | 3 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
76196 | Mirilla | Janowski | Cost Accountant | 119241 | 37270 | 3 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
80529 | Lynde | Aronson | Junior Executive | 77182 | 37270 | 4 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
80679 | Mildrid | Sokale | Geologist II | 67987 | 37270 | 4 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
84791 | Hazel | Tarbert | General Manager | 93760 | 37270 | 4 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
95213 | Cole | Kesterton | Pharmacist | 86119 | 37270 | 4 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
96513 | Theresa | Binney | Food Chemist | 47354 | 37270 | 5 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
98374 | Estrellita | Daleman | Staff Accountant IV | 70187 | 37270 | 5 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 | |
115357 | Ivy | Fearey | Structural Engineer | 92710 | 37270 | 5 | 37270 | Yovonnda | Magrannell | Executive Secretary | 63996 | 10 |
在已连接的表中选取特定列
SELECT e.employee_id,e.first_name,m.first_name AS manager
FROM employees e
JOIN employees m -- m for managerON e.reports_to = m.employee_id;
3.4 Joining Multiple Tables
对多张表进行
notebook_sql相关推荐
最新文章
- 数的补数 Number Complement
- ES6/7 异步编程学习笔记
- 移动端图片上传方法【更好的兼容安卓IOS和微信】
- 2017广西邀请赛重现赛
- 基于ADS的c语言程序设计实验,实验一:基于ADS软件传输线理论仿真设计与分析.docx...
- 怀念08,憧憬09;08盘点,09启航。
- c++自底向上算符优先分析_Python语言元素之运算符
- 大一计算机论文_大学计算机论文
- distribute-list分发列表 转自 红茶三杯sina blog
- 基于JAVA+SpringMVC+Mybatis+MYSQL的学生签到管理系统
- ZBrush中的三种对称类型的完美运用
- CarSim2017安装教程
- 面试-Iteration Owner-新加坡航空
- WebView(一)
- Nature子刊:周集中团队揭示气候变暖增强了微生物网络的复杂性与稳定性
- 商用车车队管理系统FMS
- C# / VB.NET 在PPT中创建、编辑PPT SmartArt图形
- Spring整合其他技术
- 股票自动交易接口的分类
- 5G版iPhone确定后年登场!