当前位置:网站首页>[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'
边栏推荐
- 中国芯片独角兽公司
- PHP code encryption + extended decryption practice
- Shell pass parameters
- 更改SSH端口号
- Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
- Take my brother to do the project. It's cold
- Rsync for file backup
- jwt(json web token)
- Why do you want to file? What are the advantages and disadvantages of website filing?
- ZUCC_ Principles of compiling language and compilation_ Experiment 04 language and grammar
猜你喜欢
随机推荐
ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
String to Base64
定时备份数据库脚本
How to replace the web player easyplayerproactivex Key in OCX?
Send custom events in QT
IIS build wordpress5.7 manually
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
ZUCC_ Principles of compiling language and compilation_ Experiment 01 language analysis and introduction
JUC personal simple notes
How to handle the problem that calling easycvr address integration cannot be played through easyplayer player?
Micro build low code online "quick registration applet" capability
K8S部署高可用postgresql集群 —— 筑梦之路
Increase insert speed
App Startup
Rsync for file backup
Maya re deployment
ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
Tencent conference API - get rest API & webhook application docking information
Using sonar for code checking









