Basic SQL statement exercises

background

常用的sql语句格式

Write in front
来自B站https://www.bilibili.com/video/BV1QJ411Q7pY?p=2
link

Practice notes

up主提供了视频中的sql.读者如果需要在本地练习可以下载之后,导入。 注意建库语句中的COLLATE=utf8mb4_0900_ai_ci,Mysql5.7.30还不支持,可以去掉在执行。

1. join on

Starting from 89:13 seconds of P2 video
orders
Insert picture description here
customers
Insert picture description here

SELECT order_id,orders.customer_id,first_name,last_name
FROM orders
JOIN customers
	on orders.customer_id = customers.customer_id

或者使用表别名
SELECT order_id,o.customer_id,first_name,last_name
FROM orders o
JOIN customers c
	on o.customer_id = c.customer_id


Query the order_id, orders.customer_id column in the orders table and the first_name, last_name in the customers table through the orders.customer_id = customers.customer_id of the two tables

Insert picture description here

order_items
Insert picture description here
products
Insert picture description here

SELECT order_id,oi.product_id,quantity,oi.unit_price
FROM order_items oi
JOIN products p
	on oi.product_id = p.product_id

Insert picture description here
3张表

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

Insert picture description here

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

Insert picture description here

select *
from order_items oi
join order_item_notes oin
	on oi.order_id = oin.order_id
	and oi.product_id = oin.product_id
select 
	o.order_id,
    o.order_date,
    c.first_name as customer
from orders o
join customers c
	on o.customer_id =  c.customer_id
join shippers sh
	on o.shipper_id = sh.shipper_id

Insert picture description here
使用left jion可以查看所有订单的快递员信息

select 
	o.order_id,
    o.order_date,
    c.first_name as customer,
    sh.name as shipper,
    os.name as status 
from orders o
join customers c
	on o.customer_id =  c.customer_id
left join shippers sh
	on o.shipper_id = sh.shipper_id
join order_statuses os
	on o.status = os.order_status_id

Insert picture description here

using

当join的表中有相同的列名时,可以使用using简化sql

select * 
from order_items oi
join order_item_notes oin
	on oi.order_id = oin.order_id and
		oi.product_id = oin.product_id

select * 
from order_items oi
join order_item_notes oin
	using (order_id,product_id)

自定义一个status列,该列的显示全部为'Archived'

select 
	order_id,
    order_date,
    'Archived' as status
from orders
where order_date < '2019-01-01'

Insert picture description here
根据判断条件(时间),status列显示不同的值(订单状态)

select 
	order_id,
    order_date,
    'Active' as status
from orders
where order_date >= '2019-01-01'
union
select 
	order_id,
    order_date,
    'Archived' as status
from orders
where order_date < '2019-01-01'

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
WITH ROLLUP:在group分组字段的基础上再进行统计数据。
Insert picture description here
Insert picture description here
Insert picture description here
找重复行
Insert picture description here

Guess you like

Origin blog.csdn.net/baidu_34007305/article/details/111060921