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

Introduction to encryption of grub boot program in Linux

Table of contents 1. What is grub encryption 2. g...

Vue implements verification whether the username is available

This article example shares the specific code of ...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

js canvas realizes random particle effects

This article example shares the specific code of ...

js implements the classic minesweeper game

This article example shares the specific code of ...

Solution to MySQL 8.0 cannot start 3534

MySQL 8.0 service cannot be started Recently enco...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

GET POST Differences

1. Get is used to obtain data from the server, wh...

9 great JavaScript framework scripts for drawing charts on the web

9 great JavaScript framework scripts for drawing ...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...

Complete list of CentOS7 firewall operation commands

Table of contents Install: 1. Basic use of firewa...

How to expand the disk size of a virtual machine

After Vmvare sets the disk size of the virtual ma...

Pure JS method to export table to excel

html <div > <button type="button&qu...

Detailed tutorial for downloading, installing and configuring MySQL 5.7.27

Table of contents 1. Download steps 2. Configure ...

Detailed explanation of the setting of background-image attribute in HTML

When it comes to pictures, the first thing we thi...