当前位置:网站首页>[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'
边栏推荐
- 小黑ai4code代码baseline啃食1
- DataX User Guide
- How to handle the problem that calling easycvr address integration cannot be played through easyplayer player?
- Battle history between redis and me under billion level traffic
- ZUCC_ Principles of compiling language and compilation_ Experiment 02 fsharp Ocaml language
- Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
- dataX使用指南
- [real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
- Several schemes of PHP code encryption
- There was an error checking the latest version of pip
猜你喜欢
uniapp 热更新后台管理
How to improve the customer retention rate in the operation of independent stations? Customer segmentation is very important!
js中通过key查找和更新对象中指定值的方法
liunx服务器 telnet 带用户名 端口登陆方法
ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
[micro services ~nacos] Nacos service providers and service consumers
Opencv实现图像的基本变换
Two methods of QT exporting PDF files
Jenkins自动化部署,连接不到所依赖的服务【已解决】
JUC personal simple notes
随机推荐
数据平台简介
Shell pass parameters
K8S部署高可用postgresql集群 —— 筑梦之路
How to configure networkpolicy for nodeport in kubernetes
Lombok use
成为IEEE学生会员
One development skill a day: how to establish P2P communication based on webrtc?
lombok 使用
PHP代码加密的几种方案
leetcode 1268. Search Suggestions System(搜索推荐系统)
什么是SRE?一文详解SRE运维体系
QT writing security video monitoring system 36 onvif continuous movement
After interviewing and tutoring several children, I found some problems!
Ordinary token
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
ZUCC_编译语言原理与编译_实验04 语言与文法
ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
win11在cmder中使用vim查看内容的时候空白
[xinliu-s6 new model +sa 3-star Xinghai] the new two-way server of the third generation chip was launched and the product was updated~
Jenkins自动化部署,连接不到所依赖的服务【已解决】