当前位置:网站首页>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;

 Insert picture description here

Select a specific column

SELECT name, id
FROM transportation;

 Insert picture description here

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;

 Insert picture description here
use " " rename :

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

 Insert picture description here

The structure of the display table

DESCRIBE transportation

 Insert picture description here

(2) Filtering and sorting data

Filter

• Use WHERE Clause , Filter out rows that do not meet the criteria

SELECT * 
FROM transportation 
WHERE salary>8000;

 Insert picture description here

Comparison operations
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 !=)
Other comparison operations
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;

 Insert picture description here

IN

Use IN The operation displays the values in the list

SELECT *
FROM transportation
WHERE id IN (1,2);

 Insert picture description here

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 %';

 Insert picture description here

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

 Insert picture description here

NULL

Use IS (NOT) NULL Judge null

SELECT name
FROM transportation
WHERE id IS NOT NULL;

 Insert picture description here

Logical operations
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;

 Insert picture description here

OR

OR Requirement or relationship is true

SELECT * 
FROM transportation 
WHERE salary>8000 
OR id = 1;

 Insert picture description here

NOT

The literal meaning is :“ Not ”

SELECT * 
FROM transportation 
WHERE id
	  NOT IN(1,2);

 Insert picture description here

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;

 Insert picture description here

(3) Group function

Grouping functions act on a set of data , And return a value for a set of data

Group function type
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);

 Insert picture description here

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;

 Insert picture description here

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;

 Insert picture description here

COUNT(expr) return expr Total number of records that are not empty

SELECT COUNT(name)
FROM transportation
WHERE id = 1;

 Insert picture description here

(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;

 Insert picture description here

Included in GROUP BY Columns in Clause need not be included in SELECT In the list

SELECT AVG(salary)
FROM transportation
GROUP BY id;

 Insert picture description here

Use multiple columns to group

stay GROUP BY Clause contains multiple columns

SELECT id, name, SUM(salary)
FROM transportation
GROUP BY id, name;

 Insert picture description here

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 :

  1. Rows have been grouped
  2. Group functions are used
  3. 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;

 Insert picture description here

(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;

 Insert picture description here


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;

 Insert picture description here

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;

 Insert picture description here

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;	

 Insert picture description here
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;	

 Insert picture description here
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;	

 Insert picture description here

join Connection summary

(1) The left outer join

SELECT <select_list>
FROM A
LEFT JOIN B
ON A.key=B.key

 Insert picture description here
(2) Internal connection

SELECT <select_list>
FROM A
INNER JOIN B
ON A.key=B.key

 Insert picture description here
(3) Right connection

SELECT <select_list>
FROM A
RIGHT JOIN B
ON A.key=B.key

 Insert picture description here

There will be more content to be added later , I will also keep updating !!!

原网站

版权声明
本文为[YHSevenWater]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221208252454.html