Problems with join queries and subqueries in MySQL

Problems with join queries and subqueries in MySQL

Basic syntax for multi-table joins

Multi-table connection is to combine several tables into one table and then query it

select field1, field2, ...
from table1 {inner|lift|right} join table2
on connection condition;

There are two tables: department table and employee table

Cross join and Cartesian product phenomenon

Cross Connect

Cross join, also known as unconditional inner join/Cartesian join

Each item in the first table will be combined with each item in the other table in sequence.

select * from employee,department; 

The above result is definitely not what we want to know. Each person in the left table has 4 departments. A careful look at these 4 records is exactly the result of matching each record in the left table with the right table one by one.

Cartesian product phenomenon

The reason for the Cartesian product phenomenon: there is no valid connection condition between the two tables. Since you don't have a join condition, the first row in this table can definitely be matched one-to-one with all the rows in the other table. Similarly, the second row in this table can definitely be matched one-to-one with all the rows in the other table. And so on, the last row m in this table can also be matched one-to-one with all the rows in the other table. If the other table has n rows, then the number of rows displayed at the end must be m*n rows.

If you do not want to produce Cartesian product phenomenon, you need to add effective table join conditions. Taking the above example, the left table dep_id represents their department only when it is equal to the right table id.

Inner Join

INNER JOIN is to find the intersection of several tables, that is, to filter out the correct results based on the conditions.

select emp.id,emp.name,emp.age,emp.dep_id,emp.gender,dep.id,dep.name
from employee as emp INNER JOIN department as dep
on emp.dep_id=dep.id; 

Since there is no department with id=5 in the department table, the record with dep_id=5 in the employee table is not returned; and since there are no employees行政部, this record is not returned either.

Outer Join

Left Outer Join

The left join is based on the left table. If there is no suitable record in the right table, it is filled with NULL . Its essence is to add records that have results in the left table but not in the right table on the basis of the inner join (in the case of inner join, the records in this case will be ignored).

select emp.id,emp.name,emp.age,emp.dep_id,emp.gender,dep.id,dep.name
from employee as emp left join department as dep
on emp.dep_id=dep.id; 

Right Outer Join

The opposite of the left join, the right join is based on the right table. If some fields in the left table do not have suitable results, they are filled with NULL . Its essence is to add records that have results in the right table but not in the left table on the basis of the inner join (in the case of inner join, records in this case will be ignored).

select emp.id,emp.name,emp.age,emp.dep_id,emp.gender,dep.id,dep.name
from employee as emp right join department as dep
on emp.dep_id=dep.id; 

Full outer join

Full outer join, based on inner join, displays all records of the left and right tables, and the default records in the left and right tables are filled with NULL .

There is no FULL JOIN syntax for full outer join in MySQL, but it is implemented with the help of UNION/UNION ALL statements.

The difference between UNION and UNION ALL is that UNION has the function of deduplication.

select emp.id,emp.name,emp.age,emp.dep_id,emp.gender,dep.id,dep.name
from employee as emp left join department as dep
on emp.dep_id=dep.id
union
select emp.id,emp.name,emp.age,emp.dep_id,emp.gender,dep.id,dep.name
from employee as emp right join department as dep
on emp.dep_id=dep.id; 

Subqueries

  • A subquery is a query method that nests a query statement in another query statement:
  • The inner query result of the subquery can be used as the outer query statement to provide query conditions.
  • A subquery can contain keywords such as IN , NOT IN , AND , ALL , EXISTS , and NOT EXISTS .

Subqueries can also contain comparison operators such as = , != , > , < , etc.

-- Query the name of the department with an average age of more than 20 select name
from department
where id in (
select dep_id
from employee
group by dep_id
having avg(age) > 20);

-- Query the name of the employee in the Finance Department select name 
from employee
where dep_id in (
select id 
from department 
where name='Finance Department');


-- Query the age and name of all employees older than the average age select name,age 
from employee 
where age > (
select avg(age) from employee); 

This is the end of this article about the problems of join queries and subqueries in MySQL. For more relevant MySQL join queries and subqueries, 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:
  • Detailed explanation of MySQL multi-table join query
  • What kinds of MYSQL connection queries do you know?
  • The principle and application of MySQL connection query
  • Mysql join query syntax and examples
  • Detailed explanation of the principles and usage examples of MySQL join query, union query, and subquery
  • Detailed explanation of Mysql self-join query example
  • MySQL multi-table join query example explanation
  • Detailed explanation of mysql connection query

<<:  Implementation steps for installing Redis container in Docker

>>:  Detailed explanation of the differences between the four types of positioning in CSS

Recommend

Ideas and practice of multi-language solution for Vue.js front-end project

Table of contents 1. What content usually needs t...

Detailed explanation of mysql trigger example

Table of contents What is a trigger Create a trig...

Detailed explanation of the use of the <meta> tag in HTML

In the web pages we make, if we want more people ...

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

How to understand JS function anti-shake and function throttling

Table of contents Overview 1. Function debounce 2...

WeChat applet development practical skills: data transmission and storage

Combining the various problems I encountered in m...

Docker uses Supervisor to manage process operations

A Docker container starts a single process when i...

Several ways to use v-bind binding with Class and Style in Vue

Adding/removing classes to elements is a very com...

Comprehensive understanding of line-height and vertical-align

Previous words Line-height, font-size, and vertica...

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by map...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...

Summary of basic operations for MySQL beginners

Library Operations Query 1.SHOW DATABASE; ----Que...

Five ways to traverse JavaScript arrays

Table of contents 1. for loop: basic and simple 2...

How to install suPHP for PHP5 on CentOS 7 (Peng Ge)

By default, PHP on CentOS 7 runs as apache or nob...

IE9beta version browser supports HTML5/CSS3

Some people say that IE9 is Microsoft's secon...