select advanced usage and multi-table joins
1. Select multi-clause single-table advanced practice
1.1 select multi-clause advanced syntax
- The difference between where and having is that the latter is ranked after grouping
- select syntax:
select <字段1, 字段2, ...> from <表名> [where 条件]
- other statements
group by 分组条件
: group by the specified columnhaving 条件
: Conditional judgment or filtering after groupingorder {col_name | expr | position} [ASC | DESC]
: Sort ASC ascending order, DESC descending orderLIMIT {[offset,] row_count | row_count OFFSET offset}
: limit the number of result sets
1.2 Aggregate functions
-
What is an aggregate function: an aggregate function is a prerequisite for group by
-
Common aggregate functions
serial number aggregate function usefulness 1 count() Returns the number of data in the specified group, with column names in parentheses 2 sum() Returns the sum of the data in the specified group, only for numeric columns 3 avg() Returns the average value in the specified group 4 max() Returns the maximum value of the specified data 5 min() Returns the minimum value of the specified data 6 group_concat() Return the specified data, separated by commas into one line
1.3 group by practice
- View table structure
MariaDB [world]> desc city; +-------------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | Name | char(35) | NO | | | | | CountryCode | char(3) | NO | MUL | | | | District | char(20) | NO | | | | | Population | int(11) | NO | | 0 | | +-------------+----------+------+-----+---------+----------------+
- Count the totals for each country
- mistake
select countrycode,sum(population) from city;
- correct
select countrycode,sum(population) from city group by countrycode;
- mistake
- Count the number of cities in each province of China
SELECT District,COUNT(ID) FROM city WHERE CountryCode='chn' GROUP BY District;
- Statistics of the population of each province in China
SELECT District,SUM(Population) FROM city WHERE CountryCode='chn' GROUP BY District;
1.4 having screening
- Purpose: to filter after group by
- example
- Count the number of cities and the list of cities in each province of China, and output more than ten cities
SELECT district,COUNT(*),GROUP_CONCAT(NAME) FROM city WHERE countrycode='CHN' GROUP BY district having count(*)>10;
- Count the number of cities and the list of cities in each province of China, and output more than ten cities
1.5 order by sorting
- Query the urban population of China and sort the output by population
SELECT NAME,Population FROM city WHERE CountryCode= 'CHN' ORDER BY Population DESC;
- Count the total population of each province in China, filter and output the total population of more than 1000w, and sort the output from large to small
SELECT District,SUM(Population) FROM city WHERE CountryCode= 'CHN' GROUP BY District HAVING SUM(Population)>10000000 ORDER BY SUM(Population) DESC;
1.6 limit
- Function and syntax: used to display the specified number of data rows, generally used after order by sorting, for example, select top3 or the last 3
- example
- Display the first two lines:
limit 2
- Display lines 3-6:
LIMIT 2,5
- Display the first two lines:
2. Multi-table join
- What is a multi-table join: join multiple tables into one table with the same field
- Classification of connections
- Syntax:
select 字段1,字段2 from 表1 join 表2 on
(Note that the field representation method at this time should be: table.field) - For practice, please refer to: https://www.cnblogs.com/oldboy666/p/15637461.html
- example
- Query the names of the students taught by teacher oldboy
SELECT teacher.tname,GROUP_CONCAT(student.sname) FROM teacher JOIN course ON teacher.tno=course.tno JOIN sc ON course.cno=sc.cno JOIN student ON sc.sno=student.sno WHERE teacher.tname='oldboy' GROUP BY teacher.tno;
- Query the average score of the courses taught by oldboy
SELECT teacher.tname,AVG(sc.score) FROM teacher JOIN course ON teacher.tno=course.tno JOIN sc ON course.cno=sc.cno WHERE teacher.tname='oldboy' GROUP BY teacher.tno;
- Query the names of the students taught by teacher oldboy
SELECT teacher.tname,GROUP_CONCAT(student.sname) FROM teacher JOIN course ON teacher.tno=course.tno JOIN sc ON course.cno=sc.cno JOIN student ON sc.sno=student.sno WHERE teacher.tname='oldboy' GROUP BY teacher.tno;
- Query the names of the students taught by teacher oldboy