当前位置:网站首页>MySQL_ Query of data processing
MySQL_ Query of data processing
2022-06-22 12:51:00 【YHSevenWater】
The query of data processing is divided into the following steps for learning :
● Basic SELECT sentence
● Filtering and sorting data
● Group function
● Group query
● Multi-table query
(1) Basic SELECT sentence
• SELECT Identifies which columns to select
• FROM Identifies which table to choose from
Select all columns
SELECT *
FROM transportation;

Select a specific column
SELECT name, id
FROM transportation;

| Be careful |
|---|
| SQL Language case insensitive |
| SQL It can be written on one or more lines |
| Keywords cannot be abbreviated or separated |
| Each clause should be written separately |
| Use indentation to improve the readability of statements |
Alias of column
• Rename a column
• Easy to calculate
• Keep up with the column name , You can also add keywords between column names and aliases ‘AS’ , Aliases use double quotes , To include spaces or special characters in the alias and be case sensitive
use AS rename :
SELECT name AS N, id AS I
FROM transportation;

use " " rename :
SELECT name "N", salary*12 "Annual Salary"
FROM transportation;

The structure of the display table
DESCRIBE transportation

(2) Filtering and sorting data
Filter
• Use WHERE Clause , Filter out rows that do not meet the criteria
SELECT *
FROM transportation
WHERE salary>8000;

| The operator | meaning |
|---|---|
| == | be equal to ( No ==) |
| > | Greater than |
| >= | Greater than 、 be equal to |
| < | Less than |
| <= | Less than 、 be equal to |
| <> | It's not equal to ( It can also be !=) |
| The operator | meaning |
|---|---|
| BETWEEN…AND… | Between two values ( Including boundaries ) |
| IN(set) | Equal to one of the values in the list |
| LIKE | Fuzzy query |
| IS NULL | Null value |
BETWEEN
Use BETWEEN Operation to display the value in an interval ( Closed interval )
SELECT *
FROM transportation
WHERE salary BETWEEN 8000 AND 9000;

IN
Use IN The operation displays the values in the list
SELECT *
FROM transportation
WHERE id IN (1,2);

LIKE
• Use LIKE The operation selects similar values
• Selection criteria can contain characters or Numbers :
– % Represents zero or more characters ( Arbitrary characters )
– _ Represents a character
SELECT name
FROM transportation
WHERE name LIKE ' Small %';

SELECT name
FROM transportation
WHERE name LIKE '_ Chen ';

NULL
Use IS (NOT) NULL Judge null
SELECT name
FROM transportation
WHERE id IS NOT NULL;

| The operator | meaning |
|---|---|
| AND | Logical Union |
| OR | Logic or |
| NOT | Logical not |
AND
AND The relationship between requirements and is true
SELECT *
FROM transportation
WHERE salary>8000
AND id = 3;

OR
OR Requirement or relationship is true
SELECT *
FROM transportation
WHERE salary>8000
OR id = 1;

NOT
The literal meaning is :“ Not ”
SELECT *
FROM transportation
WHERE id
NOT IN(1,2);

Sort
ORDER BY Clause
• Use ORDER BY Clause ordering
– ASC(ascend) : Ascending
– DESC(descend) : Descending
• ORDER BY Clause in SELECT End of statement
SELECT *
FROM transportation
WHERE salary BETWEEN 8000 AND 9000
ORDER BY id DESC;

(3) Group function
Grouping functions act on a set of data , And return a value for a set of data
| type | meaning |
|---|---|
| AVG() | Average |
| COUNT() | Count |
| MAX() | For maximum |
| MIN() | For the minimum |
| SUM() | Sum up |
AVG( The average )、SUM( total )
It can be done to Numerical data Use AVG and SUM function
SELECT AVG(salary),SUM(salary)
FROM transportation
WHERE id IN(1,2,3);

MAX( Maximum )、MIN( minimum value )
It can be done to Any type of data Use MIN and MAX function
SELECT MAX(salary),MIN(salary)
FROM transportation;

COUNT( Count )
COUNT(*) Returns the total number of records in the table , Apply to Any type of data
SELECT count(*)
FROM transportation
WHERE id = 1;

COUNT(expr) return expr Total number of records that are not empty
SELECT COUNT(name)
FROM transportation
WHERE id = 1;

(4) Group query
GROUP BY Clause
stay SELECT All columns in the list that are not included in the group function should be included in GROUP BY clause
SELECT id, AVG(salary)
FROM transportation
GROUP BY id;

Included in GROUP BY Columns in Clause need not be included in SELECT In the list
SELECT AVG(salary)
FROM transportation
GROUP BY id;

Use multiple columns to group
stay GROUP BY Clause contains multiple columns
SELECT id, name, SUM(salary)
FROM transportation
GROUP BY id, name;

Illegal use of group functions
• Can't be in WHERE Use group function in clause
• Can be in HAVING Use group function in clause
Filter grouping :HAVING Clause
Use HAVING Filter grouping :
- Rows have been grouped
- Group functions are used
- Satisfy HAVING The grouping of conditions in the clause will be displayed
SELECT id, name, SUM(salary)
FROM transportation
GROUP BY id, name
HAVING SUM(salary)>=9000;

(5) Multi-table query
Cartesian collection Error situation of :
select count(*) from transportation;
Output 4 That's ok
select count(*) from car;
Output 4 That's ok
Final output :4*4=16 That's ok
SELECT COUNT(*)
FROM transportation,car;

The cartesian assembly occurs under the following conditions :
– Omit connection condition
– Connection condition invalid
– All rows in all tables are interconnected
• To avoid Cartesian sets , Can be in WHERE Add a valid connection condition
• stay WHERE Clause
• When there are the same columns in the table , Prefix the column name with the table name
SELECT name,carName FROM transportation,car
WHERE transportation.id = car.id;

The table alias
• Using aliases can simplify queries
• Using a table name prefix can improve execution efficiency
SELECT `name`,carName
FROM transportation ts,car c
WHERE ts.id = c.id;

Join multiple tables
• Connect n Tables , Need at least n-1 Connection conditions . for example : Join three tables , At least two join conditions are required .
join Connect
• classification :
– Internal connection [inner] join on
– External connection
The left outer join left [outer] join on
Right connection right [outer] join on
Use ON Clause to create a connection
• In natural connection, columns with the same name are used as connection conditions
• have access to ON Clause specifies additional join conditions
• This connection condition is separate from other conditions
• ON Clause makes the statement more readable
Use ON Clause to create a multi table join :
Internal connection :
SELECT `name`,carName,transportation_salary AS ts_salary,car_salary AS c_salary
FROM transportation ts
INNER JOIN car c
ON ts.id = c.id;

The left outer join :
SELECT `name`,carName,transportation_salary AS ts_salary,car_salary AS c_salary
FROM transportation ts
LEFT JOIN car c
ON ts.id = c.id;

Right connection :
SELECT `name`,carName,transportation_salary AS ts_salary,car_salary AS c_salary
FROM transportation ts
RIGHT JOIN car c
ON ts.id = c.id;

join Connection summary
(1) The left outer join
SELECT <select_list>
FROM A
LEFT JOIN B
ON A.key=B.key

(2) Internal connection
SELECT <select_list>
FROM A
INNER JOIN B
ON A.key=B.key

(3) Right connection
SELECT <select_list>
FROM A
RIGHT JOIN B
ON A.key=B.key

There will be more content to be added later , I will also keep updating !!!
边栏推荐
- 定金预售的规则思路详解
- 【mysql】where 和 having 的区别
- Final of the 11th Blue Bridge Cup embedded design and development project
- SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值
- SAP 开发Keys 申请SSCR Keys申请
- Universaldependencies dependency label interpretation
- Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
- Tianyi cloud digital government smart data center has passed the certification
- Ogg12 processing ogg-01163 fault error
- MySQL_创建和管理表
猜你喜欢

SAP-ABAP-如何实时传输物料主数据,供应商主数据,工单,采购订单等信息给外部系统-隐式增强。

Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting

Shutter & flame - tankcombat game console development (I)

ffmpeg将amr格式转成mp3格式

Flutter&Flame——TankCombat游戏手柄开发(一)

Recommend a virtual machine software for fast cluster building of M1 chip computers

Flutter——实现网易云音乐的Tabbar切换效果

第十一届 蓝桥杯 嵌入式设计与开发项目 决赛

Sequoiadb distributed database may 2022 issue

SAP-ABAP-BAPI_GOODSMVT_CREATE创建物料凭证bapi的各种情况如何赋值
随机推荐
Ffmpeg converts AMR format to MP3 format
SAP-ABAP-SE14丢失的数据如何恢复
8 challenges of BSS application cloud native deployment
Final of the 11th Blue Bridge Cup embedded design and development project
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions
【mysql】用sql求中位数
[QT] QT get standard system path
Sliding conflict handling effect of cloud music imitating Netease
Flutter imitates airbnb's price range filter. (I)
第十二届 蓝桥杯 嵌入式设计与开发项目 决赛
Sap-abap- how to open a local file
Flutter:剥离StatefulWidget——简化页面开发、跳转以及传值
XML file parsed by repo
ONNX调研
SAP-ABAP-如何用WEBAPI的方式调用外部接口
胡润研究院首发中国元宇宙潜力企业榜,巨杉数据库入选未来之星企业
函数指针和指针函数的区别
SequoiaDB分布式数据库2022.5月刊
Flutter——实现网易云音乐的渐进式卡片切换