当前位置:网站首页>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
边栏推荐
- 月薪1万在中国是什么水平?答案揭露残酷的收入真相
- What is chain game system development? How to make chain game system development
- 复旦大学EMBA2022毕业季丨毕业不忘初心 荣耀再上征程
- C # simulation lottery
- Analysis and solution of data and clock mismatch delay in SPI transmission
- How does win11's own drawing software display the ruler?
- Promise date
- 国债年化利率太低了,有比国债逆回购年化利率还要高的理财产品吗?
- 可验证随机函数 VRF
- Golang review summary
猜你喜欢
![[zeloengine] summary of pit filling of reflection system](/img/7a/c85ba66c5dd05908b2d784fab306a2.png)
[zeloengine] summary of pit filling of reflection system
![[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>](/img/b2/cbba006e5d1ada959f494336e93f39.png)
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>

IAAs infrastructure cloud cloud network

Communication between processes (pipeline details)

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心

Understanding service governance in distributed development

Analysis and solution of data and clock mismatch delay in SPI transmission

月薪1万在中国是什么水平?答案揭露残酷的收入真相

从业务需求出发,开启IDC高效运维之路

Fudan University EMBA peer topic: always put the value of consumers in the most important position
随机推荐
QT ListView 列表显示组件笔记
Cookie、cookie与session区别
easyui datagrid控件使用
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
fastadmin tp 安装使用百度富文本编辑器UEditor
Slf4j and log4j2 process logs
从业务需求出发,开启IDC高效运维之路
测试框架-unittest-命令行操作、断言方法
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
2W word detailed data Lake: concept, characteristics, architecture and cases
152. Product maximum subarray
152. 乘积最大子数组
MySQL self incrementing lock
Differences between cookies, cookies and sessions
Attachment handling of SAP Fiori
解决Win10磁盘占用100%
doGet与doPost
Permission management - role assignment menu
tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
MySQL table write lock