Network security Day24-select advanced usage and multi-table joins

1. Select multi-clause single-table advanced practice

1.1 select multi-clause advanced syntax

  1. The difference between where and having is that the latter is ranked after grouping
  2. select syntax:select <字段1, 字段2, ...> from <表名> [where 条件]
  3. other statements
    1. group by 分组条件: group by the specified column
    2. having 条件: Conditional judgment or filtering after grouping
    3. order {col_name | expr | position} [ASC | DESC]: Sort ASC ascending order, DESC descending order
    4. LIMIT {[offset,] row_count | row_count OFFSET offset}: limit the number of result sets

1.2 Aggregate functions

  1. What is an aggregate function: an aggregate function is a prerequisite for group by

  2. 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;
      
  • 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;
      

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

2. Multi-table join

  • What is a multi-table join: join multiple tables into one table with the same field
  • Classification of connections
    insert image description here
  • 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;
      

Guess you like

Origin blog.csdn.net/m0_73293867/article/details/132015047