Let's talk about MySQL joint query in detail

Let's talk about MySQL joint query in detail

Union query

Union can merge tables queried by two or more select statements and eliminate duplicate rows in the tables.

The select statement must have the same number of columns with the same data type.

1. Query the ID and name of each province in China

select ProID,ProName from T_Province

2. ID and name of all prefecture-level cities in Hunan Province

select CityID,CityName from T_City
where ProID = (
    select ProID from T_Province where ProName="湖南省"
);

3. Combine them with union

select ProID,ProName from T_Province
union
select CityID,CityName from T_City
where ProID = (
    select ProID from T_Province where ProName="湖南省"
);

This results in the union of the two query results.

The column names in the combined set of a UNION are always equal to the column names in the first SELECT statement in the UNION.

Union query union all

select ProID,ProName from T_Province
union all
select CityID,CityName from T_City
where ProID = (
    select ProID from T_Province where ProName="湖南省"
);

When using union all, duplicate rows are not eliminated.

Inner join of joint query

1. Check how many prefecture-level cities there are in Hubei Province

No need for union query:

select count(CityID) from T_City
where ProID = (select ProID from T_Province where ProName="湖北省")

Join the two tables together using ProID

select ProName,CityName from(
    T_City join T_Province
    on T_City.ProID = T_Province.ProID
)
where ProName="Hubei Province"

2. Count the number of prefecture-level cities in each province and output the province name and the number of prefecture-level cities

select T_City.ProID,ProName,count(CityID) as cc from(
    T_City join T_Province
    on T_City.ProID = T_Province.ProID
)
group by T_City.ProID
order by cc desc;

The ProID to be output in the select statement should be one of T_City and T_Province, otherwise an error will be reported.

Two tables need to have a common "language" (the column names do not necessarily have to be the same) in order to be joined.

You can give tables aliases, set the alias of T_City table to tc and the alias of T_Province to tp.

select tc.ProID,ProName,count(CityID) as cc from(
    T_City tc join T_Province tp
    on T_City.ProID = T_Province.ProID
)
group by tc.ProID
order by cc desc;

3. Query cities with more than 20 districts and counties, and output the city name and the number of districts and counties

select CityName,count(DisName) disCount from (
    T_City tc join T_District td
    on tc.CityID = td.CityID
)
group by CityName
having disCount > 20;

Three-table union query

1. Which cities in which provinces are the top three cities with the most districts and counties? The query results include the province name, city name, and the number of districts and counties.

select tp.ProName,tcd.CityName,tcd.ci from
(
select ProID,CityName,count(ID) ci from(T_City tc join T_District td on tc.CityID = td.CityID) 
 
   group by tc.CityID
order by ci desc
limit 3
)tcd
join T_Province tp on tcd.ProID = tp.ProID;

Joint query left join & right join

The inner join is based on the common parts of the left and right tables.

The left join is based on the common parts of the left and right tables plus the unique parts of the left table.

A right join is based on the common parts of the left and right tables plus the unique parts of the right table.

Query all provinces and their cities information

select * from(
T_Province tp join T_City tc
on tp.ProID = tc.ProID
);

Query all provinces and their cities and provinces without cities

select * from(
T_Province tp left join T_City tc
on tp.ProID = tc.ProID
);

Query all provinces and their city information and city information without provinces

select * from(
T_Province tp right join T_City tc
on tp.ProID = tc.ProID
);

Summarize

This is the end of this article about MySQL joint query. For more relevant MySQL joint query content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL beginners can say goodbye to the troubles of grouping and aggregation queries
  • Detailed explanation of MySQL joint query optimization mechanism
  • Detailed explanation of the principles and usage examples of MySQL join query, union query, and subquery
  • Analysis of MySQL multi-table joint query operation examples
  • MySQL database aggregate query and union query operations

<<:  How to use the debouce anti-shake function in Vue

>>:  Dissecting the advantages of class over id when annotating HTML elements

Recommend

Pitfalls and solutions encountered in MySQL timestamp comparison query

Table of contents Pitfalls encountered in timesta...

Example code for realizing charging effect of B station with css+svg

difficulty Two mask creation of svg graphics Firs...

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

MySQL binlog opening steps

Binlog is a binary log file that is used to recor...

Enable sshd operation in docker

First, install openssh-server in docker. After th...

How to set the default value of a MySQL field

Table of contents Preface: 1. Default value relat...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

Detailed explanation of rpm installation in mysql

View installation and uninstallation # View rpm -...

CentOS 7 set grub password and single user login example code

There are significant differences between centos7...

Detailed explanation of CSS weight value (cascading) examples

•There are many selectors in CSS. What will happe...

Using vsftp to build an FTP server under Linux (with parameter description)

introduce This chapter mainly introduces the proc...

An article to give you a deep understanding of Mysql triggers

Table of contents 1. When inserting or modifying ...

The process of installing MySQL 8.0.26 on CentOS7

1. First, download the corresponding database fro...