当前位置:网站首页>[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'
原网站

版权声明
本文为[Ly methane]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240606321612.html