Mysql join query syntax and examples

Mysql join query syntax and examples

Connection query:

It is the result of connecting each row of two queries (or tables) in pairs, that is, a row in one table with a row in another table. Perform "horizontal docking" to get a new row.

Connection queries include the following different forms and connection methods:

Cross join, inner join, outer join (divided into: left outer join, right outer join)

Join query syntax:

select * from table name [connection method] join table name [on connection condition] where ...;

Test data:

mysql> select * from test;
+----+--------+------+------+
| id | name | sex | age |
+----+--------+------+------+
| 1 | name1 | Female | 15 |
| 2 | name1 | Female | 15 |
| 4 | name2 | Male | 30 |
| 5 | name50 | Male | 12 |
+----+--------+------+------+

mysql> select * from user;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | name1 | 18 |
| 2 | name2 | 15 |
| 3 | name3 | 20 |
| 4 | name4 | 30 |
+----+-------+------+

Cross join

1. A cross join can actually be considered as a "full version" of a join query, that is, all rows are unconditionally connected.

2. The keyword "cross" can be omitted;

3. Cross join is also called "Cartesian product", which is usually of little application value

Syntax:

select * from table name, table name;
select * from table name join table name;
select * from table name cross join table name;

Standard cross-connection writing

mysql> select * from test cross join user;
+----+--------+------+------+----+-------+------+
| id | name | sex | age | id | name | age |
+----+--------+------+------+----+-------+------+
| 1 | name1 | Female | 15 | 1 | name1 | 18 |
| 2 | name1 | Female | 15 | 1 | name1 | 18 |
| 4 | name2 | Male | 30 | 1 | name1 | 18 |
| 5 | name50 | Male | 12 | 1 | name1 | 18 |
| 1 | name1 | Female | 15 | 2 | name2 | 15 |
| 2 | name1 | Female | 15 | 2 | name2 | 15 |
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
| 5 | name50 | Male | 12 | 2 | name2 | 15 |
| 1 | name1 | Female | 15 | 3 | name3 | 20 |
| 2 | name1 | Female | 15 | 3 | name3 | 20 |
| 4 | name2 | Male | 30 | 3 | name3 | 20 |
| 5 | name50 | Male | 12 | 3 | name3 | 20 |
| 1 | name1 | Female | 15 | 4 | name4 | 30 |
| 2 | name1 | Female | 15 | 4 | name4 | 30 |
| 4 | name2 | Male | 30 | 4 | name4 | 30 |
| 5 | name50 | Male | 12 | 4 | name4 | 30 |
+----+--------+------+------+----+-------+------+

Use join directly to connect, which is equivalent to cross join. The default is cross join.

mysql> select * from test join user;

The query results are consistent with the above!!!

You can also directly from table, table... It is also equivalent to a cross connection

mysql> select * from test, user;

The query results are consistent with the above!!!

Inner join

1. Inner join is actually based on cross join, and some data is filtered out through on condition.

2. The keyword "inner" can be omitted, but it is recommended to include it. The on join condition is what really matters.

3. Inner join is the most widely used join query, and its essence is to filter out "meaningful data" based on conditions.

Syntax:

select * from table name join table name on join condition;
select * from table name inner join table name on join condition;

Find the data with the same name field in two tables

mysql> select * from test inner join user on test.name=user.name;
+----+-------+------+------+----+-------+------+
| id | name | sex | age | id | name | age |
+----+-------+------+------+----+-------+------+
| 1 | name1 | Female | 15 | 1 | name1 | 18 |
| 2 | name1 | Female | 15 | 1 | name1 | 18 |
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
+----+-------+------+------+----+-------+------+

Find the data with the same name field in two tables, and use as to give the table a different name for use

mysql> select * from test as t inner join user as u on t.name=u.name;

The query results are consistent with the above!!!

Find the data where the name field in the two tables is equal, age>15 in the test table, and age>10 in the user table

If there are repeated fields in the table, you need to use [table name.field name] to use it, for example, age:

mysql> select * from test inner join user on test.name=user.name where test.age>15 and user.age>10;
+----+-------+------+------+----+-------+------+
| id | name | sex | age | id | name | age |
+----+-------+------+------+----+-------+------+
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
+----+-------+------+------+----+-------+------+

Find the data in the two tables where the name field is equal and sex = 'male'.

If there are no duplicate fields in the table, you can use [table name.field name] or directly use [field name], for example, sex:

mysql> select * from test as t inner join user as u on t.name=u.name where t.sex='男';
mysql> select * from test as t inner join user as u on t.name=u.name where sex='男';
+----+-------+------+------+----+-------+------+
| id | name | sex | age | id | name | age |
+----+-------+------+------+----+-------+------+
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
+----+-------+------+------+----+-------+------+

Outer join - left outer join

1. The left outer join is actually a kind of join that ensures that all data in the left table can be retrieved.

2. The left outer join is actually based on the inner join, plus all the data in the left table that does not meet the conditions

3. The keyword "outer" can be omitted.

Syntax:

select * from table name left [outer] join on join condition where ...;

Take the table on the left as the benchmark to ensure that all data in the table on the left is displayed, and any unmatched data is NULL

mysql> select * from test left join user on test.name=user.name;
+----+--------+------+------+------+-------+------+
| id | name | sex | age | id | name | age |
+----+--------+------+------+------+-------+------+
| 1 | name1 | Female | 15 | 1 | name1 | 18 |
| 2 | name1 | Female | 15 | 1 | name1 | 18 |
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
| 5 | name50 | Male | 12 | NULL | NULL | NULL |
+----+--------+------+------+------+-------+------+

Outer join - right outer join

1. The right outer join is actually a kind of join that ensures that all data in the right table can be retrieved.

2. Right outer join is actually based on inner join, plus all the data in the right table that does not meet the conditions

3. The keyword "outer" can be omitted.

Syntax:

select * from table name right [outer] join on join condition where ...;

Take the table on the right as the benchmark to ensure that all data in the table on the right is displayed, and any unmatched data is NULL

mysql> select * from test right join user on test.name=user.name;
+------+-------+------+------+----+-------+------+
| id | name | sex | age | id | name | age |
+------+-------+------+------+----+-------+------+
| 1 | name1 | Female | 15 | 1 | name1 | 18 |
| 2 | name1 | Female | 15 | 1 | name1 | 18 |
| 4 | name2 | Male | 30 | 2 | name2 | 15 |
| NULL | NULL | NULL | NULL | 3 | name3 | 20 |
| NULL | NULL | NULL | NULL | 4 | name4 | 30 |
+------+-------+------+------+----+-------+------+

Summarize

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

You may also be interested in:
  • Problems with join queries and subqueries in MySQL
  • 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
  • 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

<<:  How to query whether the mysql table is locked

>>:  Linux system (Centos6.5 and above) installation jdk tutorial analysis

Recommend

Introduction to the use of MySQL performance stress benchmark tool sysbench

Table of contents 1. Introduction to sysbench #Pr...

Vue uses dynamic components to achieve TAB switching effect

Table of contents Problem Description What is Vue...

Detailed steps to use Redis in Docker

1. Introduction This article will show you how to...

Realizing the effect of carousel based on jQuery

This article shares the specific code of jQuery t...

Summary on Positioning in CSS

There are four types of positioning in CSS, which...

MySQL 8.0.15 compressed version installation graphic tutorial

This article shares the installation method of My...

30 free high-quality English ribbon fonts

30 free high-quality English ribbon fonts for down...

Summary of the understanding of virtual DOM in Vue

It is essentially a common js object used to desc...

MySQL master-slave configuration study notes

● I was planning to buy some cloud data to provid...

Layim in javascript to find friends and groups

Currently, layui officials have not provided the ...

How to convert extra text into ellipsis in HTML

If you want to display extra text as ellipsis in ...

Learning to build React scaffolding

1. Complexity of front-end engineering If we are ...

Font Treasure House 50 exquisite free English font resources Part 2

Designers have their own font library, which allo...

Detailed explanation of some settings for Table adaptation and overflow

1. Two properties of table reset: ①border-collaps...