当前位置:网站首页>MySQL query advanced - from the use of functions to table joins, do you remember?
MySQL query advanced - from the use of functions to table joins, do you remember?
2022-08-01 08:15:00 【cool autumn】
作者简介:C/C++领域新星创作者,为C++和java奋斗中
个人社区:微凉秋意社区
系列专栏:MySql一点通
推荐一款模拟面试、刷题神器注册免费刷题
前言
书接上文,Advanced operation learning queries today,包括函数、分组、分页、子查询和表连接.内容比较实用,Recommended collection or subscribe to the column is convenient to study and review.
文章目录
一、MySQL函数的使用
- mysql中内置了很多函数,Each function represents a specific functions
1、单行函数
- 特点:Each row of data will produce a corresponding results
- 表中有多少行,How many rows will be the result
- 位置:
select
之后,from
之前 Or as a condition of screening
concat(…,列名,…,列名)
Used for splicing of multiple columns values- 例如:以firstname+lastnameShow all employees, in the form of name
select concat(first_name,last_name) as 全名 from employees
- 例如:以firstname+lastnameShow all employees, in the form of name
mod(值1,值2)
求值1和值2的取余结果 ,Equivalent in mathematics %(值1%值2)- 标准SQLThe specification does not exist%Take more than writing,So take more operation should be usedmod()
- 例如:
select mod(10,3) from dual
注:dual
为虚拟表,In order to make the currentSQL符合语法规范,When they don't write to do automatic filling
length(列名|数据)
获取长度- 查询所有firstname长度>6的员工信息
select * from employees where length(first_name)>6
- 查询所有firstname长度>6的员工信息
now() | sysdate()
获取系统当前时间- select now()
select SYSDATE()
select now() from dual
- select now()
2、多行函数(组函数)
- As a group for the unit to obtain the results,A group to get a result
- If you do not manually to group,则默认整张表为一组
- sum(列名)
- For the sum of a column
- avg(列名)
- 求某一列的平均值
- max(列名)
- 求某一列的最大值
- min(列名)
- 求某一列的最小值
- count(列名)
- For a number of column values(非空)
示例:
-- Set of functions operatingsalary
select
SUM(salary) 总和,AVG(salary) 平均值,MAX(salary) 最大值,min(salary) 最小值,COUNT(salary) 非空值的数量
from employees
-- Computational performance are listed in the table is not the number of employees is empty
select COUNT(commission_pct) from employees
-- For the rows in the table
select COUNT(*) from employees
select COUNT(employee_id) from employees
二、MySQL的分组
- 在MySQLIn a group is an operating unit
- 简单分组
select 列名 from 表名 group by 列名
- An average salary of each department by department as the unit
select department_id,AVG(salary) 平均薪资 from employees group by department_id
- An average salary of each department by department as the unit
- where + 分组
-- 查询部门id为10、20、30的部门的平均薪资、
select department_id,AVG(salary) 平均薪资
from employees
where department_id in(10,20,30) -- 先筛选
GROUP BY department_id -- After grouping query
- having + 分组
select department_id,AVG(salary) 平均薪资
from employees
group by department_id -- 先分组
having department_id in(10,20,30) -- 后筛选
- 分组中 where 和 having 的区别
- whereIs to perform in front of the group,havingAfter group perform
- whereNot in the group with a set of functions,但是having可以
- When both are available,优先使用
where
,效率更高
三、Paging and query keyword execution order
limit:Used to display the query results limit article number,通常用于分页查询
select 列名 from 表名 limit According to the starting index,显示条数
- Query all employees data before ten
select * from employees limit 0,10
select * from employees limit 10
下标为0时可以省略
- Query all employees data before ten
当limitAnd other query keyword combination,limitThe last execution must be
查询的执行顺序:
语法顺序 执行顺序
select 5
from 1
where 2
group by 3
having 4
order by 6
limit 7
四、子查询
- 当一个SQLRely on anotherSQL的执行结果,在当前SQL中嵌套另一个SQL,This method is called the subquery
1、where子查询
1.1、单值子查询
- 嵌套的子SQLReturns only a line of a column of the result
-- 查询员工id为100的员工的部门id
select department_id from employees where employee_id=100
-- 子查询::查询员工id为100的员工所在的部门信息
select * from departments
where department_id=(select department_id from employees where employee_id=100)
- Prioritise the innerSQL,再执行外层SQL
- 子SQLNeed to write the brackets
1.2、多值子查询
- 嵌套的子SQLCan return multiple results
--查询firstname中包含s的员工所在的部门信息
select * from departments
where department_id in(select department_id from employees where first_name like '%s%')
2、from子查询
- 将子SQLThe query results as a temporary table for subsequent operation
-- To get the top ten employees' average salary
-- To get the highest salary first top ten employees salary
select salary from employees ORDER BY salary desc LIMIT 10
-- 子查询:再根据子SQLResults temporary query through the alias to construct a data table
select avg(salary) from (select salary from employees ORDER BY salary desc LIMIT 10) as e
五、表连接
- When the query results need to be obtained from multiple tables,You need to connect multiple tables for novelty search operation
The premise of table joins:
- There is a connection between multiple tables must be relationship(外键)
- A foreign key is usually connected to another table primary key
- In the table as a condition of connection using
1、内连接
语法:
select 表1.列名,表2.列名,...
from 表1 inner join 表2
on 连接条件
- 特点:The two tables at the same time constraint,Only when all the tables are conform to join condition,Will display information
- 使用:
inner
可省
2、(左)外连接
语法:
select 表1.列名,表2.列名,...
from 表1(左表) left outer join 表2(右表)
on 连接条件
- 特点:Only to the right table do constraint,Left the data in the table will show,Right in the table only if it meets the requirements of connection will show
- 使用:
outer
可省 - Left outer join and right outer join is
left
和right
的不同,位置不同,Usually use the left outer join - 使用:
-- Check all employee information and meet the requirements for the connection of department information
select e.*,d.*
from employees e LEFT JOIN departments d
ON e.department_id=d.department_id
全外连接:
- Merge the two queries results show
查询语句1union
查询语句2 - 使用:
-- 内连接
select e.*,d.*
from employees e inner join departments d
on e.department_id=d.department_id
union -- Internal connection and left the connection of the query results to merge
-- 左连接
select e.*,d.*
from employees e LEFT JOIN departments d
ON e.department_id=d.department_id
union
Will the merger results are去重union all
不会对结果去重- 特点:Merge both sides do not restrain the
- Merge both the number of query results in field、顺序必须一致
实际开发中,Using the high frequency is the internal connection and left outer join
3、自连接
- 概念:Is a special table join,Participate in the connection is the same table
具体使用:
- A certain relationship between the two fields in the table
-- 查询:员工id、员工姓名-firstname、Direct leadership ofid、Direct leadership of姓名
select e1.employee_id 员工id,e1.first_name 员工姓名,e2.employee_id 领导id,e2.first_name 领导姓名
from employees e1 LEFT JOIN employees e2-- e1On behalf of the employee information table,e2On behalf of the leading information table
ON e1.manager_id=e2.employee_id -- The two correlation fields as a condition of connection
- Judgment of the same field,作为连接条件
-- Query the same wages employee information
select e1.employee_id 员工1的id,e1.salary 员工1的工资,e2.employee_id 员工2的id,e2.salary 员工2的工资
from employees e1 LEFT JOIN employees e2
on e1.salary=e2.salary -- 连接条件
where e1.employee_id<e2.employee_id -- Both sides to prevent judgment is the same person,并去重
写在最后This blog isMySQLIn the blockbuster content,I hope you can properly absorb and correct use of,一起加油!!!
边栏推荐
- 力扣周赛304 6135. 图中的最长环 内向基环树
- pytest接口自动化测试框架 | 使用函数返回值的形式传入参数值
- Case practice --- Resnet classic convolutional neural network (Mindspore)
- app 自动化 通过工具查看app 元素 (三)
- Golang: go to connect and use mysql
- 22 Niu Ke Duo School 1 I. Chiitoitsu (Probability dp)
- 179. 最大数
- zip打包目录所有文件(含隐藏文件/夹)
- 请问用flinksql写入数据到clickhouse需要引入什么依赖吗?
- zip package all files in the directory (including hidden files/folders)
猜你喜欢
随机推荐
Image lossless compression software which works: try completely free JPG - C image batch finishing compression reduces weight tools | latest JPG batch dressing tools download
Golang: go open web service
Golang:go静态文件处理
日志导致线程Block的这些坑,你不得不防
2022杭电中超杯(1)个人题解
Electromagnetic compatibility introductory tutorial (6) test project
Pod环境变量和initContainer
What do the values 1, 2, and 3 in nodetype mean?
图片无损压缩软件哪个好用:试试完全免费的JPG-C 图片批量修整压缩减肥工具吧 | 最新jpg批量修整工具下载
【Unity3D】相机
LeetCode 415:字符串相加
热修复技术可谓是百花齐放
leetcode-6135:图中的最长环
leetcode-6133: maximum number of groupings
Generate pictures based on the content of the specified area and share them with a summary
codeforces每日5题(均1600)-第二十七天
基于tika实现对文件类型进行判断
LeetCode240+312+394
Case practice --- Resnet classic convolutional neural network (Mindspore)
POJ2031空间站题解