当前位置:网站首页>MySQL linked table query, common functions, aggregate functions
MySQL linked table query, common functions, aggregate functions
2022-07-25 16:32:00 【Sooner or later, I will go bald】
mysql Joint table query 、 Common functions 、 Aggregate functions
1. Even the table query
matters needing attention :
1. Join table query is because the data you need to get is in two tables , A table cannot meet our needs
example : An employee table , There are employee names and employees id Two fields , Another table is the payroll, which contains employees id Now my requirement is to get the name and corresponding salary of each employee in the company , At this time, we need to use the linked table query –>
2. Join table query is to connect two tables through a certain relationship , If there is no valid connection , It will happen –>
Cartesian product phenomenon –>
surface a Yes m Row data , surface b Yes n That's ok –>
select * from a , b where 1=1–>
1=1 It's an ineffective connection because 1 Is equal to 1 All this is invalid connection –>
shortcoming :( No advantages )–>
1. It may cause Caton ,–>
If the table a Yes 10000 Data , surface b Also have 10000 Data, then if we define invalid connection conditions at this time, it will be queried and displayed 1 Ten million pieces of data –>
2. Trouble finding demand data –>
For example, the employee's name and salary I mentioned above , If no valid connection is established, then , One employee corresponds to multiple salaries, so we can't get the data we want –>
Connection classification :
Internal connection :
Equivalent connection :
Non equivalent connection
Self join
External connection :
The left outer join
Right connection
Full outer join
Cross connect
There are many connections, so I'll pick some commonly used ones
Equivalent connection
① The result of equivalent connection of multiple tables is the intersection part of multiple tables
②n Table joins , Need at least n-1 Connection conditions
③ There is no requirement for the order of multiple tables
④ Generally, you need to alias a table
⑤ It can be used with all the clauses mentioned above , Sorting for example 、 grouping 、 Screening
example : Query the corresponding salary of each employee
select name,money from yuangong , gomgzi where yuangong.id=gomgzi.id
Here we can use alias to make the code look simpler
select name,money from yuangong y , gomgzi g where y.id=g.id
External connection
1、 The query result of the external connection is all records in the main table. If there are matching records from the table , If there is no matching value from the table , Is displayed null External connection query results = Internal connection result + Records in the master table but not in the slave table
2、 The left outer join ,left join On the left is the main watch Right connection ,right join On the right is the main watch
3、 Left outer and right outer exchange the order of two tables , Can achieve the same effect
4、 Full outer join = The result of internal connection + surface 1 Yes, but the watch 2 There is no the + surface 2 Yes, but the watch 1 There is no the
example
select * from Main table a left outer join From the table b on a.id=b.id where a.id is not null
select a.*,b.* from usera a left outer join grade b on a.id=b.uid where a.id is not null
Common function
Character functions
| effect | function | result |
|---|---|---|
| Turn lowercase | LOWER(‘SQL Course’) | sql course |
| Turn capitalization | UPPER(‘SQL Course’) | SQL COURSE |
| Splicing | CONCAT(‘Hello’, ‘World’) | HelloWorld |
| Intercept | SUBSTR(‘HelloWorld’,1,5) | Hello |
| length | LENGTH(‘HelloWorld’) | 10 |
| Character occurrence index value | INSTR(‘HelloWorld’, ‘W’) | 6 |
| Second half of character interception | TRIM(‘H’ FROM ‘HelloWorld’) | elloWorld |
| Character substitution | REPLACE(‘abcd’,‘b’,‘m’) | amcd |
Number function
| effect | function | result |
|---|---|---|
| rounding | ROUND(45.926, 2) | 45.93 |
| truncation | TRUNC(45.926, 2) | 45.92 |
| Seeking remainder | MOD(1600, 300) | 100 |
Date function
| effect | function | result |
|---|---|---|
| Get current date | now() | Current date time |
| Convert the character of date format to the date of specified format | STR_TO_DATE(‘9-13-1999’,’%m-%d-%Y’) | 1999-09-13 |
| Convert date to character | DATE_FORMAT(‘2018/6/6’,‘%Y year %m month %d Japan ’) | 2018 year 06 month 06 Japan |
Aggregate functions
1、 SUM(): Sum up .
2、 AVG(): averaging .
3、MAX(): For maximum .
4、 MIN(): For the minimum .(1、2、3、4、 Often with GROUP BY Use it together , It can also be used alone )
5、 COUNT(): Count the number of records . Often with GROUP BY Use it together , It can also be used alone If there are filter conditions plus keywords having
Merge
1、UNION: Merge all the query results , Then remove the same records
2、UNION ALL: Merge all the query results , The same records will not be removed
边栏推荐
- Which led display manufacturer is better
- 城市燃气安全再拉警钟,如何防患于未“燃”?
- Who moved my memory and revealed the secret of 90% reduction in oom crash
- Communication between processes (pipeline details)
- MySQL implicit lock
- 伦敦银K线图的各种有用形态
- 2W word detailed data Lake: concept, characteristics, architecture and cases
- tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
- 复旦大学EMBA2022毕业季丨毕业不忘初心 荣耀再上征程
- Fastadmin TP installation uses Baidu rich text editor ueeditor
猜你喜欢

What is the shortcut key for win11 Desktop Switching? Win11 fast desktop switching method

MyBaits

Win11动态磁贴没了?Win11中恢复动态磁贴的方法

如何安装govendor并打开项目

【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码

Verifiable random function VRF

Win11桌面切换快捷键是什么?Win11快速切换桌面的方法

进程之间的通信(管道详解)

IaaS基础架构云 —— 云网络
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]
随机推荐
从业务需求出发,开启IDC高效运维之路
Test Driven Development (TDD) online practice room | classes open on September 17
QT ListView 列表显示组件笔记
Talk about how to use redis to realize distributed locks?
Intention lock
Slf4j and log4j2 process logs
优必选大型仿人服务机器人Walker X的核心技术突破
论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)
The presentation logic of mail sending and receiving inbox outbox and reply to the problem of broken chain
IaaS基础架构云 —— 云网络
01.一个更简单的方法来传递大量的props
百度富文本编辑器UEditor 图片宽度100%自适应,手机端
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
MySQL metadata lock (MDL)
Typescript learning 1 - data types
Promise期约
ILSSI认证|六西格玛DMAIC的历程
Who moved my memory and revealed the secret of 90% reduction in oom crash
Rebudget汇报PPT
【obs】发送前丢帧及帧优先级