Simple sorting of MySQL query knowledge (Part 1)

1. Conditional query

(1). String = query of a certain value. Check the ability to filter conditions such as:

select device_id,university
from user_profile
where university = "北京大学"

(2). The column in which is greater than the value>, which can be regarded as an operation judgment, including <, =, in, between, etc.

select device_id,gender,age,university
from user_profile
where age > 24

(3). Test between, in mysql, it is included on both sides

3.1

select device_id,gender,age from user_profile where age between 20 and 23;

3.2 Note where age >=20 and age<=23 Do not miss the age after the logical operator AND

select device_id,gender,age
from user_profile
where age >=20 and age<=23

(4). Determine the set, which is used to find the tuple whose attribute value belongs to the specified set.

# select device_id,gender,age,university from user_profile where university <> '复旦大学'# <> 不等于 。!=是不等于的另一种写法。
# select device_id,gender,age,university from user_profile where university != '复旦大学'#用了where子句和运算符!
select device_id,gender,age,university from user_profile where university NOT IN ("复旦大学")
# where not university = '复旦大学'
# where university not like '复旦大学'

The format is: column name [NOT] IN (constant 1, constant 2,...constant n) # remember to use between constants, separated

IN: When the value in the column is equal to a constant value in IN, the result is True, indicating that this record is a record that meets the query conditions.

NOT IN: When the value in the column is equal to a certain constant value, the result is False, indicating that this record is a record that does not meet the query conditions.

Example 17. Query all class numbers and class names whose class names are "electronic information engineering technology", "electronic audio-visual" or "electronic assembly technology".

SELECT 班号, 班名 FROM 班级表
WHERE 班名 IN ('电子信息工程技术' , '电子声像‘, '电子组装技术')

Equivalent to:

SELECT 班号,班名 FROM 班级表
WHERE 班名= ‘电子信息工程技术’ OR
班名 = ‘电子声像’ OR
班名 = ‘电子组装技术’

Example 18. The query class name is neither the class number and class name of "electronic information engineering technology", nor "electronic audio-visual" and "electronic assembly technology".

SELECT 班号,班名 FROM 班级表
WHERE 班名 NOT IN ('电子信息工程技术' , '电子声像‘, '电子组装技术')

Equivalent to:

SELECT 班号,班名 FROM 班级表
WHERE 班名!= ‘电子信息工程技术’ AND
班名!= ‘电子声像’ AND
班名!= ‘电子组装技术’

Supplement: where constraints

Where clauses can be used: comparison operators: > >= < <= <> !=

between 80 and 100 - Values ​​between 80 and 100

in(80,90,100) - value is 80 or 90 or 100

like 'e%' The wildcard can be % or _, % means any number of characters _ means one character

Logical operators: Logical operators and or not can be used directly in multiple conditions

(5). Queries involving null values, filtering of null values, the original intention of the title is to examine the use of null.

In practice, an empty string also results in a null value, so in normal operation.

SELECT device_id,gender,age,university 
from user_profile 
where age!=''
或者
SELECT device_id,gender,age,university 
from user_profile 
where age<>''
或者
SELECT device_id,gender,age,university 
from user_profile 
where age is not null

Queries involving null values, which represent indeterminate values ​​in the database.

For example, when students have taken courses and have not yet taken an exam, these students have course enrollment records but no test scores, so the test scores are null.

To determine whether a value is a NULL value, ordinary comparison operators cannot be used.

5.1 The statement format for judging that the value is empty is:

Column name IS NULL

5.2 The statement format for judging that the value is not empty is:

Column name IS NOT NULL

Example 19. Query the student numbers and corresponding course numbers of students who have not yet taken the exam.

SELECT 学号,课程号 FROM 成绩表
WHERE 成绩 IS NULL

Example 20. Query the student number and course number of all students who have taken the exam.

SELECT 学号,课程号 FROM 成绩表
WHERE 成绩 IS NOT NULL

(6).Multiple conditional query, logical operators AND and OR can be used in the WHERE clause to form a multi-conditional query

Example 21. Query all class numbers and class names whose department number is greater than 1 and whose class name starts with "electronics"

SELECT 班号,班名 FROM 班级表
WHERE 系号>1 AND 班名 LIKE '电子%’

Example 22. Query the student number, name, gender and class number of all boys in classes 11212P and 11214D

SELECT 学号,姓名,性别,班号 FROM 学生表
WHERE (班号= ‘ 11212P ’ OR 班号= ‘ 11214D ’) AND 性别=‘男’

can also be written as:

SELECT 学号,姓名,性别,班号 FROM 学生表
WHERE 班号 IN( ‘ 11212P ’ , ‘ 11214D ’) 
AND 性别=‘男’

(7). Fuzzy query. Character matching, field name like 'matching content'. The general form is: column name [NOT ] LIKE

The matching string can contain the following four wildcards:

_: match any character;

%: Match 0 or more characters;

[ ]: Match any character in [ ] (if the characters to be compared are continuous, you can use a hyphen "-" to express);

[^ ]: ^The sharp colon means not, which means negation; it does not match any character in [ ].

Tips: A question often asked in interviews: What database optimization techniques do you know? SQL statement optimization is also a part of database optimization, and our like fuzzy query will cause a full table scan, which is relatively slow, so we should try to avoid using the like keyword for fuzzy query.

Example 23. Query the detailed information of the student whose surname is 'Zhang' in the student table

SELECT * FROM 学生表 WHERE 姓名 LIKE ‘张%’

Example 24. Query the names of students whose surname is "Zhang" and whose first name is 3 characters.

SELECT * FROM 学生表 WHERE 姓名 LIKE '张__’

If the type of the name column is changed to nchar(20), there is no result in SQL Server 2012. The reason is that the type of the name column is char(20). When the name is less than 20 Chinese characters, the system will automatically add a space after the data when storing the data. The space is regarded as a character and also participates in the comparison of LIKE. You can use rtrim() to remove right spaces

SELECT * FROM 学生表 WHERE rtrim(姓名) LIKE '张__'

Example 25. Query the situation of the students whose surnames are 'Zhang', 'Li' and 'Liu' in the student table.

SELECT * FROM 学生表 WHERE 姓名 LIKE '[张李刘]%’

Example 26. Query the name and student number of the student whose first name in the student table is "small" or "big".

SELECT 姓名,学号 FROM 学生表 WHERE 姓名 LIKE '_[小大]%'

Example 27. Query all students whose surname is not "Liu" in the student table.

SELECT 姓名 FROM 学生 WHERE 姓名 NOT LIKE '刘%’

Example 28. Query the student information whose last digit of the student number is not 2, 3, or 5 from the student table.

SELECT * FROM 学生表 WHERE 学号 LIKE '%[^235]'

2. Advanced query

(1) Summarize data using aggregation functions

The statistical functions provided by SQL are:

COUNT([Shift+8]): Count the number of tuples in the table;

COUNT([DISTINCT] <column name>): Count the number of column values ​​in this column;

SUM(<column name>): Calculate the sum of column values;

AVG(<column name>): Calculate the average value of the column value;

MAX(<column name>): Find the maximum value of the column;

MIN( <column name> ): Find the minimum value of the column value.

In the above functions except COUNT([Shift+8]), other functions ignore NULL values ​​in the calculation process.

Statistical functions cannot appear in the WHERE clause.

For example, it is wrong to query the student number of the student with the highest grade as follows:

SELECT Student ID FROM Transcript

WHERE grade = MAX(grade)

Example 29. Count the total number of students.

SELECT COUNT(*) FROM 学生表

Example 30. Count the number of students who have taken courses.

SELECT COUNT (DISTINCT 学号)
FROM 成绩表

Example 31. Calculate the sum of the total test scores of the students whose student number is "11214D24".

SELECT SUM(成绩) FROM 成绩表
WHERE 学号 = ‘11214D24 '

Example 32. Calculating the test grade average of students in the course "M01F011"

SELECT AVG(成绩) FROM 成绩表
WHERE 课程号 = ‘M01F011 ‘

Example 33. Query the highest and lowest scores of the course "M01F011"

SELECT MAX(成绩) 最高分, 
MIN(成绩) 最低分 FROM 成绩表
WHERE 课程号 = ‘M01F011 '

Replenish:

ROUND() function

The ROUND function is used to round a numeric field to a specified number of decimal places.

SQL ROUND() Syntax

round (which value, how many decimal places are reserved). SQL does not distinguish between single and double quotes

(2) Regarding group by, perform group calculation on query results

Function: You can control the level of calculation: for the whole table or for a group.

Purpose: To refine the object of the calculation function.

The general form of a grouping statement:

[GROUP BY ]

[HAVING ]

The grouping column in the GROUP BY clause must be the column name existing in the table, and the alias of the result set column assigned by the AS clause cannot be used. Having can directly use the alias. But it seems that the new version of Mysql supports aliases directly after group by and have?

Only grouping columns or statistical functions can appear in the query list of the SELECT statement with the GROUP BY clause, because each group returns only one row of results after grouping.

Example: Title: Now the operation wants to analyze the active status and number of posts of users of different genders in each school. Please calculate the number of users of each gender in each school, the average number of active days within 30 days, and the average Number of posts.

SELECT gender,university,COUNT(*)user_num,AVG(active_days_within_30)avg_active_days,
AVG(question_cnt)avg_quesition_cnt
FROM user_profile
GROUP BY gender,university

Example 34. Count the number of people enrolled in each course, and list the course number and number of people.

SELECT 课程号, COUNT(课程号) AS 选课人数
FROM 成绩表
GROUP BY 课程号

This statement first groups the query results by the value of the course number, and all tuples with the same course number value are grouped together, and then use the COUNT function for each group to calculate the number of students in each group.

Example 35. Query the number of courses and average grades of each student.

SELECT 学号, 
COUNT(*) 选课门数,
AVG(成绩) 平均成绩
FROM 成绩表
GROUP BY 学号

Expansion of interview questions : In grouped SQL statements, what is the execution order of select, from, where, group by, and having?

The writing order of sql statements cannot be reversed: select···from···where···group by···having···order by···limit···

Answer: The general execution order is from···where···group by···having···select···order by···limit···;

The correct ordering should be like this:

First from (to assemble data from different data sources, which data table needs to retrieve data from),

Then to where (based on the specified conditions, filter the data),

Then to group by (dividing the filtered data into multiple groups),

Then to having (the condition for filtering the data that has been grouped above), it functions a bit like the WHERE clause, but it is used for groups instead of individual records,

Finally select (see which column in the result set, or the calculation result of the column)

order by : In what order to view the returned data.

The first thing is to determine which table the data comes from, and then filter the data according to the where condition, and then perform group by grouping (there can be multiple grouping conditions, grouping in order of fields), after grouping, the result set is grouped by having After filtering, the data is presented.

Supplement: select column a, aggregate function (aggregate function specification) from table name where filter condition group by column a

1. The group by clause is also used in combination with the where conditional statement. When combined, where comes first and group by follows. That is, first filter the record collection of select xx from xx with where, and then use group by to group the filtered results.

2. Use the having clause to filter the grouped results, the syntax is similar to where: having conditional expression

Need to pay attention to the usage difference between having and where:

1. Having can only be used after group by to filter the grouped results (that is, the prerequisite for using having is grouping).

2.where must be before group by, that is, before having.

3. Aggregation functions are not allowed in the conditional expression after where, but having can. HAVING is usually used with the GROUP BY clause

When where, group by, having, and order by appear in a query statement at the same time, the execution order and writing order are:

1. Execute where xx to filter the data in the entire table and return the first result set.

2. Use group by grouping for the first result set and return the second result set.

4. Execute having xx for the second collection to filter and return the third result set.

3. Execute select xx for each set of data in the third result set, and execute several times for as many sets as there are, and return the fourth result set.

5. Sort for the fourth result set.

The difference between distinct and group by

distinct

distinct can only be placed at the top of the query field, and cannot be placed in the middle or behind the query field.

distinct works on all the following fields, that is, deduplication refers to the data that is completely repeated in all fields in the query, not only the data that is repeated on a single field connected after distinct.

To query multiple fields, but only for one field to deduplicate, it is impossible to use distinct deduplication.

group by

It is generally used with clustering functions (such as count()/sum(), etc.), and can also be used alone.

group by also works on all the following fields, that is, deduplication is the data that is completely repeated in all fields of the query, not only the data that is repeated on a single field connected after group by

3. Multi-table query - multi-table join

If a query involves two or more tables at the same time, it is called a join query.

Join queries are the most important queries in relational databases.

Join queries include inner joins, outer joins, and cross joins.

The conditions used in a join query to join two tables are called join conditions or join predicates.

The general format is:

inner join

The inner join syntax is as follows:

SELECT …
FROM 表名
[INNER] JOIN 被连接表
ON 连接条件

Example 39. Querying the details of each student and their class

SELECT * FROM 学生表
INNER JOIN 班级表 ON 学生表.班号=班级表.班号

There is a duplicate column in the result: class number.

Example 40. Remove the duplicate columns in Example 39.

SELECT 学号, 姓名,班级表.班号, 班名 FROM 学生表 JOIN 班级表
ON 学生表.班号=班级表.班号

例41.查询重修学生的修课情况,要求列出学生的名字、所修课的课程号和成绩。

SELECT 姓名, 课程号, 成绩
FROM 学生表 JOIN 成绩表
ON 学生表.学号 = 成绩表.学号
WHERE 状态 = '重修'

执行连接操作的过程

首先取表1中的第1个元组,然后从头开始扫描表2,逐一查找满足连接条件的元组,

找到后就将表1中的第1个元组与该元组拼接起来,形成结果表中的一个元组。 表2全部查找完毕后,再取表1中的第2个元组,然后再从头开始扫描表2, …

重复这个过程,直到表1中的全部元组都处理完毕为止。注:如果为表指定了别名,则查询语句中其他所有用到表名的地方都要使用别名

Guess you like

Origin blog.csdn.net/m0_73485816/article/details/129451345