当前位置:网站首页>MySQL add, delete, modify, query & advanced query statements
MySQL add, delete, modify, query & advanced query statements
2022-07-23 11:27:00 【_ Leaf1217】
Catalog
One 、 Preface
We read the last article together MySQL Account management , There are also basic operations such as database and table creation , Let's take a look at this article MySQL Some of the basics CRUD Statements and some more advanced queries , We use this employee form for the whole process :t_mysql_employees

Two 、 Basic query
1.1 increase
-- increase
insert into t_mysql_employees
values(207,'Xiao','Leaf','CHAO','520.1234.1217','AD_PRES',100000,0.9,null,110,'2003-12-17')-- Query test
select * from t_mysql_employees where last_name = 'Leaf';
Query results :

1.2 modify
-- modify
update t_mysql_employees set employee_id = 001 where last_name = 'Leaf';
-- Query test
select * from t_mysql_employees where last_name = 'Leaf';
Query results :

1.3 Delete
-- Delete
delete from t_mysql_employees where last_name = 'Leaf';-- Query test
select * from t_mysql_employees where last_name = 'Leaf';
Query results :

1.4 Basic query
#1. A single field in a query table
select job_id from t_mysql_employees;
#2. Multiple fields in the query table
select job_id,email from t_mysql_employees;
#3. Query all fields in the table
select * from t_mysql_employees;
#4. Query constant value
SELECT 100;
SELECT 'john';
#5. Query expression
SELECT 100%98;
#6. Query function
SELECT VERSION();Here, these basic queries don't put too many screenshots of query results ~
1.5 Take the alias
#7. names
select email Mailbox number from t_mysql_employees;result :
1.6 duplicate removal
#8. duplicate removal :distinct
# Case study : Query all department numbers involved in the employee table
select DISTINCT job_id from t_mysql_employeesresult :

Be careful :
MySQL in :+ The role of No
java Medium + Number :
① Operator , Both operands are numeric
② Connector , As long as one of the operands is a string
mysql Medium + Number :
Just one function : Operator
1.7 Query the current time
-- Query the current time
select now();Query results :

3、 ... and 、 Filter query
3.1 One 、 Filter by conditional expression
# Case study 1: Query salary >12000 Employee information
select * from t_mysql_employees where salary > 1200;Query results :

# Case study 2: Query department number is not equal to 90 The employee name and department number of the
Two ways of writing :
-- How to write it 1
select last_name,department_id from t_mysql_employees
where department_id <> 90;
-- How to write it 2
select last_name,department_id from t_mysql_employees
where not(department_id = 90);Query results :

3.2 Filter by logical expression
# Case study 1: Check salary at 10000 To 20000 Between the employee's name 、 Salary and bonus
select last_name,salary,commission_pct from t_mysql_employees
where salary between 10000 and 20000;Query results :

# Case study 2: The inquiry department number is not in 90 To 110 Between , Or pay more than 15000 Employee information
select * from t_mysql_employees
where not(department_id between 90 and 110) or salary > 1500;Query results :

Four 、 Fuzzy query
4.1 like
# Case study 1: Query employee name contains characters a Employee information
select * from t_mysql_employees where last_name like '%a%'# Case study 2: The third character in the query employee name is e, The fifth character is a The name and salary of the employee
select last_name,salary from t_mysql_employees where last_name like '_e_a%'# Case study 3: The second character in the employee name is _ Employee name of
notes :ESCAPE It's an escape , Give Way $ Take a place .
select last_name from t_mysql_employees where last_name like '_$_%' ESCAPE '$'Query results :

4.2 between and
# Case study 1: Query the employee number in 100 To 120 Employee information between
select * from t_mysql_employees where employee_id between 100 and 120; Query results :

4.3 in
# Case study : The job number of the employee is IT_PROG、AD_VP、AD_PRES An employee's name and job number in the
select last_name,job_id from t_mysql_employees
where job_id in ('IT_PROG','AD_VP','AD_PRES');Query results :
4.4 is null && is not null
# Case study 1: Query the employee name and bonus rate without bonus
select last_name,commission_pct from t_mysql_employees where commission_pct is null;Query results :

# Case study 2: Check the name and bonus rate of employees with bonus
select last_name,commission_pct from t_mysql_employees
where commission_pct is not null;Query results :

4.5 Safety is equal to
# Case study 1: Query the employee name and bonus rate without bonus
select first_name,commission_pct from t_mysql_employees
where commission_pct <=> nullQuery results :

# Case study 2: Query salary as 12000 Employee information
select * from t_mysql_employees where salary <=> 12000;Query results :

Let's compare is null and Safety is equal to <=> Characteristics :
IS NULL: Can only judge NULL value , High readability , It is recommended to use
<=>: You can judge NULL value , You can also judge ordinary values , Less readable
5、 ... and 、 Sort query
5.1 order by Clause
5.1.1、 Sort by a single field
# Case study : Sort by employee table salary
select last_name,salary from t_mysql_employees order by salary desc #2、 Add filter criteria and sort
# Case study : Check department number >=90 Employee information , And in descending order of employee number
select * from t_mysql_employees where department_id >= 90 order by employee_id desc #3、 Sort by expression
# Case study : Query employee information In descending order of annual salary
select em.salary*12*(1+ifnull(commission_pct,0)),em.* from t_mysql_employees em
order by salary*12*(1+ifnull(commission_pct,0)) descQuery results :

#4、 Sort by alias
# Case study : Query employee information In ascending order of annual salary
select * from t_mysql_employees order by salary*12*(1+ifnull(commission_pct,0)); #5、 Sort by function
# Case study : Query employee name , And in descending order of the length of the names
select last_name,CONCAT(first_name,last_name),length(CONCAT(first_name,last_name))
from t_mysql_employees order by length(CONCAT(first_name,last_name)) desc;Query results :
#6、 Sort by multiple fields
# Case study : Query employee information , It is required that the salary be in descending order first , Press again employee_id Ascending
select * from t_mysql_employees order by salary desc,employee_id asc;Query results :

OK, These are Leaf Bring about MySQL Related sentences of , Basic query , You can pay attention to the progress together if you like ! I'll see you next time !!!
边栏推荐
- Web Component-自定義元素的生命周期
- 自定义MVC(下)
- Understanding of closures of JS
- 大厂面试机器学习算法(5)推荐系统算法
- How pycharm packages OCR correctly and makes the packaged exe as small as possible
- Last child does not take effect
- js的事件执行机制(Event loop)
- Spectral clustering | Laplace matrix
- Application of higher-order functions: handwritten promise source code (II)
- Differences and basic operations of shell/sh/bash
猜你喜欢

Precautions for realizing "real-time data response" on the page

The problem that GBK codec cannot decode may be caused by the file name

动态设置卡片的主题色

General Query & paging code

自定义MVC的使用&增删改查
D2DEngine食用教程(2)———绘制图像

Machine learning algorithm for large factory interview (6) time series analysis

NepCTF 2022 MISC <签到题>(极限套娃)

大厂面试机器学习算法(5)推荐系统算法
![[pytho-flask筆記5]藍圖簡單使用](/img/0a/00b259f42e2fa83d4871263cc5f184.png)
[pytho-flask筆記5]藍圖簡單使用
随机推荐
NPM init vite app < project name > error install for[‘ [email protected] ‘] failed with code 1
构造函数,原型链,instanceOf
[python flask notes 5] blueprint is easy to use
C语言中的流氓goto语句
Pytorch white from zero uses North pointing
Two sorting and one random data fetching of stored procedures
some、every、find、findIndex的用法
JS higher order function
Spectral clustering | Laplace matrix
[pytho-flask筆記5]藍圖簡單使用
自定义MVC的使用&增删改查
Command Execution Vulnerability and defense
Simple implementation of rectangular area block
composer的一些操作
js高阶函数
sqli-lab第17~22关通关随笔记
How pycharm packages OCR correctly and makes the packaged exe as small as possible
js的事件执行机制(Event loop)
laravel api接口+令牌认证登录
[C language] what is a function? Classification and emphasis of functions (help you quickly classify and remember functions)