ZZZZZZ目的
ZZZZZZ代码
ZZZZZZ重点
ZZZZZZ操作(非代码,需要自己手动)
8- CASE运算符The CASE Operator_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1UE41147KC?p=62&vd_source=eaeec77dfceb13d96cce76cc299fdd08
- 在sql_store中,根据orders表格,将里面所有的订单旁边都有一个标签”status“,如果订单是今年的,就叫Active(活跃的),如果是去年的,就叫“Last Year”,再以前的的就叫archived(存档),如果是未来的订单,就叫"Future",并输出订单id,订单日期和status
USE sql_store;
SELECT order_id,
CASE
WHEN YEAR(NOW()) = YEAR(order_date) THEN 'Active'
WHEN YEAR(NOW()) -1 = YEAR(order_date) THEN 'Last Year'
WHEN YEAR(NOW()) -1 > YEAR(order_date) THEN 'Archived'
ELSE 'Future'
END AS category
FROM orders
IF函数只允许单一的测试表达式
【练习题】
输出下面这张图片,其中,如果积分超过3000,就是金牌顾客,如果在2000-3000(包含2000和3000)之间,是银牌顾客,如果低于2000,那么是青铜客户
USE sql_store;
SELECT CONCAT(first_name, ' ', last_name) AS customer,
points,
CASE
WHEN points > 3000 THEN 'Gold'
WHEN points BETWEEN 2000 AND 3000 THEN 'Silver'
ELSE 'Bronze'
END AS category
FROM customers
ORDER BY points DESC
这里的第二行WHEN也可以写成WHEN points >= 2000 THEN 'Sliver',因为大于3000的都已经返回Gold了,进行第二行WHEN的只能是小于3000的,所以可以不用写小于3000这个条件
【第八章】1- 创建视图 | Creating Views「视图」_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1UE41147KC?p=63&vd_source=eaeec77dfceb13d96cce76cc299fdd08
- 很多操作的代码其实很相像,所以如果每次都要重新输入代码的话,那肯定会花费很多时间,这里可以用创建视图这个方法来解决
将下面这段代码加入视图中
USE sql_invoicing;
SELECT c.client_id, c.name, SUM(invoice_total) AS total_sales
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id, name
USE sql_invoicing;
CREATE VIEW sales_by_client AS
SELECT c.client_id, c.name, SUM(invoice_total) AS total_sales
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id, name
然后,点击SCHEMAS右上角的刷新按钮,然后在sql_invoicing中的Views可以找到新建的sales_by_client,也可以将这个视图看作是一个表格,可以调用这个表格里面的内容,但是视图并不储存数据,我们的数据存储在了表中,我们只是把视图提供给了一张基础表
【练习题】
在sql_invoicing中,显示每位客户的结余,并建立名为“客户结余client_balance”的视图,这个视图中要出现客户id,客户名字,结余(balance)
USE sql_invoicing;
CREATE VIEW client_balance AS
SELECT i.client_id, c.name, SUM(i.invoice_total - i.payment_total) AS balance
FROM invoices i
JOIN clients c USING (client_id)
GROUP BY client_id
2- 更改或删除视图 | Altering or Dropping Views_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1UE41147KC?p=64&vd_source=eaeec77dfceb13d96cce76cc299fdd08
- DROP VIEW :删除视图
DROP VIEW sales_by_client
名为sales_by_client的视图就被删除了 - CREATE OR REPLACE VIEW ... AS:修改某个视图,如果没有该视图,则创建一个
例如,将sales_by_client修改为计算每个客户的发票总支付
USE sql_invoicing;
CREATE OR REPLACE VIEW sales_by_client AS
SELECT c.client_id, c.name, SUM(payment_total) AS total_payments
FROM clients c
JOIN invoices i USING (client_id)
GROUP BY client_id, name - 点击sales_by_client,然后按ctrl+s即可将该视图保存下来,或者可以点击sales_by_client旁边的工具按钮,即可打开编辑模式,不过里面的代码可能跟原本我们输入的不太一样,这时MySQL系统自己弄的,目的是防止名称冲突,不管管这个
例如,在代码中加入ORDER BY total_sales DESC,然后点击屏幕右下角的Apply,然后再点击Apply,Finsh即可应用
最好还是将视图储存在SQL文件中,并放入源代码控制
3- 可更新视图 | Updatable Views_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1UE41147KC?p=65&vd_source=eaeec77dfceb13d96cce76cc299fdd08
- 如果视图里面没有DISTINCT函数,任何聚合函数(MIN、MAX、SUM、GROUP BY、 HAVING、UNION),那么就可以更新视图,主要用INSERT、UPDATE、DELETE函数来更新视图
- 根据invoices表,创建一个名为invoices_with_balance的视图,视图里面跟invoices一样,但是多了balance这一列,这一列是invoice_total和payment_total的差值,而且只输出右结余的发票
CREATE OR REPLACE VIEW invoices_with_balance AS
SELECT invoice_id, number, client_id, invoice_total, payment_total,
invoice_total - payment_total AS balance,
invoice_date, due_date, payment_date
FROM invoices
WHERE invoice_total - payment_total > 0 - 删除invoices_with_balance中发票id为1的发票
DELETE FROM invoices_with_balance
WHERE invoice_id = 1 - 将invoices_with_balance中二号发票的到期日推迟两天
UPDATE invoices_with_balance
SET due_date = DATE_ADD(due_date, INTERVAL 2 DAY)
WHERE invoice_id = 2 - 只有当视图右所有基础表中要用的列,插入的新发票才会生效,例如,如果视图中没有发票日期列,就不能往视图里插图任何行,因为我们的表不允许插入的日期列为空值