MySQL database_data query_self-association, subquery

MySQL database

Self-association

  • Design province information table structure provinces
	id
	ptitle
  • Design the table structure of city information
	id
	ctitle
	proid
  • The proid of the citys table represents the province to which the city belongs, corresponding to the id value of the provinces table

problem:

能不能将两个表合成一张表呢?

Thinking:

观察两张表发现,citys表比provinces表多一个列proid,其它列的类型都是一样的.

significance:

存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大。

answer:

	定义表areas,结构如下
	
	id
	atitle
	pid

Description:

  • Because the province has no province to which it belongs, it can be filled in as null
  • The pid of the province to which the city belongs, fill in the number id corresponding to the province
  • This is self-association. A column in the table is associated with another column in the table, but their business logic meanings are different. The pid of the city information refers to the id of the province information
  • In this table, the structure remains the same, you can add information such as districts, counties, towns, streets, villages and communities

The statement to create the areas table is as follows:

create table areas(
    aid int primary key,
    atitle varchar(20),
    pid int
);

  • Import data from SQL file

source areas.sql; # 'areas.sql' 文件是一个插入全国省市区数据的sql文件

  • Query how many provinces there are in total

select count(*) from areas where pid is null;

  • Example 1: Query all cities where the name of the province is "Shanxi Province"
select city.* from areas as city
inner join areas as province on city.pid=province.aid
where province.atitle='山西省';
  • Example 2: Query all districts and counties whose city name is "Guangzhou City"
select dis.* from areas as dis
inner join areas as city on city.aid=dis.pid
where city.atitle='广州市';

Subquery

  • What is a subquery?
  • In a select statement, another select statement is embedded, then the embedded select statement is called a subquery statement.

Main query
主要查询的对象,第一条 select 语句

The relationship between the main query and the subquery

  • The subquery is embedded in the main query
  • Subquery is auxiliary to the main query, either as a condition or as a data source
  • Subqueries can be independent of the statement is a complete selectsentence

Subquery classification

  • Scalar subquery: The result returned by the subquery is a data (one row and one column)
  • Column subquery: The returned result is one column (one column with multiple rows)
  • Row subquery: The result returned is one row (one row with multiple columns)

Scalar quantum query

  • Query the average age of students in a class
  • Query students older than the average age
--查询班级学生的平均身高

select * from students where age > (select avg(age) from students);

Column-level subqueries

  • Query the names of all the classes in which the student is in the class
    • Find out all the class ids in the student table
    • Find the corresponding name in the class table
select name from classes where id in (select cls_id from students);

Row-level subqueries

  • Needs: Find the oldest and tallest student in the class
  • Row element: Combine multiple fields into one row element, which will be used in row-level subqueries
select * from students where (height,age) = (select max(height),max(age) from students);

Use of specific keywords in subqueries

  • in range
    • Format: main query where condition in (column subquery)

to sum up

Plus previous articles on basically the MySQl 查询knowledge points came to a close.
Let's take a look查询的完整格式

SELECT select_expr [,select_expr,...] [      
      FROM tb_name
      [WHERE 条件判断]
      [GROUP BY {col_name | postion} [ASC | DESC], ...] 
      [HAVING WHERE 条件判断]
      [ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
      [ LIMIT {[offset,]rowcount | row_count OFFSET offset}]
]
  • Complete selectsentence
select distinct *
from 表名
where ....
group by ... having ...
order by ...
limit start,count
  • The order of execution is:

    • from table name
    • where …
    • group by …
    • select distinct *
    • having …
    • order by …
    • limit start,count
  • In actual use, it is just a combination of some parts of the sentence, not all of it!

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90454625