这是我参加[第四届青训营]笔记创作活动的第8天。
今天是项目研究开发,学习爬虫。以及数据库mysql的学习。 首先我们看一些基础的语法操作。 第一部分:
SELECT *
FROM customers
WHERE phone is null
-- 选择出空的语句
-- ORDER BY first_name DESC(降序)
-- 什么都没有的话就是按照序列默认排序
-- limit 6,3代表选择的是7,8,9序号的行
-- limit 3代表前三行
第二部分:
-- select order_id,o.customer_id,first_name,last_name
-- from orders o
-- join customers c
-- on o.customer_id = c.customer_id
-- join就是连接,on后面是连接的条件。
-- 外连接直接加前缀就可以啦 比如:sql_inventory.products连接到别的数据库的表格
-- 自连接,自己和自己连接
-- use sql_hr;
-- select
-- e.employee_id,
-- e.first_name,
-- m.first_name as manager
-- from employees e
-- join employees m
-- on e.reports_to = m.employee_id
-- 多表格连接
-- 就是多加几个join
实例演练: 要求将下面的三个表连接在一起。
代码:
use sql_invoicing;
select
payment_id,
p.date,
c.name as client_nmae,
pm.name as payment_methods
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
结果:
第三部分:
隐式连接和非隐式连接的写法:
select *
from orders o
join customers c
on o.customer_id = c.customer_id
-- Implpicit Join Syntax
select *
from orders o,customers c
where o.customer_id = c.customer_id
第四部分:
外连接:
use sql_store;
select
c.customer_id,
c.first_name,
o.order_id
from customers c
left join orders o
-- right连接就是右边的表格范围更大一点,与内连接的区别就是交集和一半补集
on c.customer_id = o.customer_id
order by c.customer_id
尽量使用左连接,使得整个代码的逻辑看起来更加清晰。
自外连接:
示例代码:
use sql_hr; select e.employee_id, e.first_name, m.first_name as manager from employees e left join employees m on e.reports_to = m.employee_id
结果:
| 33391 | D'arcy | Yovonnda | |
|---|---|---|---|
| 37270 | Yovonnda | ||
| 37851 | Sayer | Yovonnda | |
| 40448 | Mindy | Yovonnda | |
| 56274 | Keriann | Yovonnda | |
| 63196 | Alaster | Yovonnda | |
| 67009 | North | Yovonnda | |
| 67370 | Elladine | Yovonnda | |
| 68249 | Nisse | Yovonnda | |
| 72540 | Guthrey | Yovonnda | |
| 72913 | Kass | Yovonnda | |
| 75900 | Virge | Yovonnda | |
| 76196 | Mirilla | Yovonnda | |
| 80529 | Lynde | Yovonnda | |
| 80679 | Mildrid | Yovonnda | |
| 84791 | Hazel | Yovonnda | |
| 95213 | Cole | Yovonnda | |
| 96513 | Theresa | Yovonnda | |
| 98374 | Estrellita | Yovonnda | |
| 115357 | Ivy | Yovonnda | |
| --- | --- | ||