当前位置:网站首页>[10 day SQL introduction] Day2
[10 day SQL introduction] Day2
2022-06-24 08:43:00 【Ly methane】
1873. Calculate special bonuses
surface : Employees
+-------------+---------+
| Name | type |
+-------------+---------+
| employee_id | int |
| name | varchar |
| salary | int |
+-------------+---------+
employee_id It's the primary key of this table .
Each row of this table shows the employees id , Name and salary .
Write a SQL Query statement , Calculate the bonus for each employee . If an employee's id It's odd and his name doesn't start with 'M' start , Then his bonus is his salary 100%, Otherwise, the bonus is 0.
For the returned result set, please follow employee_id Sort
Analysis of the answer
IF expression
IF( expr1 , expr2 , expr3)
If expr1 Go back to expr2, Otherwise return to expr3
IFNULL(expr1 , expr2)
If expr1 yes null, return expr2, Otherwise return to expr1
SELECT employee_id,
IF(MOD(employee_id, 2) != 0 AND LEFT(name, 1) != 'M', salary, 0) bonus
FROM Employees
ORDER BY employee_id
627. Changing gender
Salary surface :
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| id | int |
| name | varchar |
| sex | ENUM |
| salary | int |
+-------------+----------+
id It's the primary key of this table .
sex The value of this column is ENUM type , Only from ('m', 'f') To take .
Please write a SQL Query to exchange all 'f' and 'm' ( namely , Will all 'f' Turn into 'm' , vice versa ), Use only Single update sentence , And there is no intermediate temporary table .
Analysis of the answer
update Basic use :
update surface set Field 1 = ‘ value 1’ where Field 2=‘ value 2’
UPDATE Salary SET sex = IF(sex = 'm', 'f', 'm')
196. Delete duplicate email
surface : Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| email | varchar |
+-------------+---------+
id Is the primary key column of the table . Each row of the table contains an email . Email will not contain uppercase letters .
Write a SQL Delete statements to Delete All duplicate emails , Keep only one id The smallest and only email . With In any order Return result table .
Analysis of the answer
DELETE Basic use
DELETE FROM table1 WHERE table1.id = 1
DELETE Delete the associated table , Inner join clause usage : t1 INNER JOIN t2 ON Connection condition
DELETE table1
FROM table1 INNER JOIN table2 ON table1.id = table2.id
WHERE table2.type = 'a' AND table1.id = 2
This sentence SQL intend table1 and table2 Left click id Make internal connections (ID The same line ), Delete type yes ‘a’ And id yes 2 Of
This problem requires a self - join , To perform a self join , You can use inner join or left join clauses
You can set the table according to email Same connection , Then delete p1.id > p2.id The line of
DELETE p1
FROM Person p1 INNER JOIN(Person p2) ON p1.email = p2.email
WHERE p1.id > p2.id
You can also write like this :
Both watches are their own , Make internal connections , No, ON Conditions , Equivalent to Cartesian product , Then delete email Same and p1.id > p2.id Of
DELETE p1
FROM Person p1 INNER JOIN(Person p2)
WHERE p1.email = p2.email AND p1.id > p2.id
review + summary
SELECT usage
The most basic :
SELECT Name FROM indicate WHERE Conditions
Expression usage
IF(expr1, expr2, expr3) expr1 by true Then return to expr2, Otherwise return to expr3
IFNULL(expr1, expr2) expr1 by NULL Then return to expr2, Otherwise return to expr1
ISNULL(expr1) expr1 yes NULL Then return to 1, Otherwise return to 0
NOT IN(list) be not in list Inside
UPDATE usage
The most basic : WHERE Find records that meet the criteria , Then change one of the fields
UPDATE surface SET Field 1=" value 1" WHERE Field 2=" value 2"
Connection usage
a,b It's a watch 1, surface 2 Another name for
surface 1 a INNER JOIN( surface 2 b) ON Connection condition ( Not writing a condition is a Cartesian product )
DELETE usage
The most basic : Find out and delete the fields with certain values in the table
DELETE FROM surface WHERE Field =" value "
DELETE Delete the associated table , Delete table t1 in id by 2 And t2 Table has id For the same 2 The type is ’a‘ The record of
DELETE t1
FROM t1 INNER JOIN(t2) ON t1.id = t2.id
WHERE t1.id = 2 AND t2.type = 'a'
边栏推荐
- Battle history between redis and me under billion level traffic
- liunx 更改 vsftpd 的端口号
- Shell basic operators -- relational operators
- 2022春招面试总结
- Common date formatter and QT method for obtaining current time
- 相机投影矩阵计算
- Fundamentals of 3D mathematics [17] inverse square theorem
- How to implement approval function in Tekton
- After interviewing and tutoring several children, I found some problems!
- Shell basic operator -- arithmetic operator
猜你喜欢

ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing

jwt(json web token)

ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar

Permission model DAC ACL RBAC ABAC

ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language

ZUCC_编译语言原理与编译_实验03 编译器入门

表单图片上传在Chorme中无法查看请求体的二进制图片信息

5 minutes, excellent customer service chat handling skills

JUC personal simple notes

Base64编码详解及其变种(解决加号在URL变空格问题)
随机推荐
JUC personal simple notes
How to replace the web player easyplayerproactivex Key in OCX?
MATLAB Camera Calibrator相机标定
Markdown to realize text link jump
【生活思考】计划与自律
Numpy 中的方法汇总
Tencent conference API - get rest API & webhook application docking information
api平台通用签名机制
[micro services ~nacos] Nacos service providers and service consumers
Live broadcast appointment: growth of Mengxin Product Manager
New technology practice, encapsulating the permission application library step by step with the activity results API
pymysql 向MySQL 插入数据无故报错
QTimer定时器不起作用的原因
2021-06-25: a batch of strings consisting only of lowercase letters (a~z) are put
dataX使用指南
"Wechat cloud hosting" first practical battle | introduction to minimalist demo
mysql组合索引的有序性
【力扣10天SQL入门】Day2
ZUCC_编译语言原理与编译_实验04 语言与文法
Ordering of MySQL composite index