Detailed explanation of MySQL multi-table join query

Detailed explanation of MySQL multi-table join query

Multi-table join query

The connection between tables is divided into inner connection and outer connection

  • Inner join: select only the records that match each other in two tables
  • Outer join: includes both matching records and unmatched records in two tables. Outer join is divided into left outer join (left join) and right outer join (right join)

Inner Join

First, prepare two tables

Student table

insert image description here

Score table

insert image description here

Inner join: Find the common records that meet the conditions in each table. Query the student names and scores in the student table.

The first way to write: only use where

select a.s_name, b.s_score from student a,score b where a.s_id = b.s_id;

The second way to write it: join ... on ...

select a.s_name, b.s_score from student a join score b on a.s_id = b.s_id

The third way to write it: inner join … on…

select a.s_name, b.s_score from student a inner join score b on a.s_id = b.s_id

insert image description here

Left Join

Left join: According to the records in the left table, find the records that meet the conditions in the connected right table to match them. If no records matching the left table are found, null is used to represent them.

The first way to write it: left join … on …

select a.s_name,b.s_score from student a left join score b on a.s_id = b.s_id

The second way of writing: left outer join … on …

select a.s_name,b.s_score from student a left outer join score b on a.s_id = b.s_id

insert image description here

Right Join

**Right join: **According to the records in the right table, find the records that meet the conditions in the connected left table to match them. If no match is found, it is represented by null.

The first way to write it: right join … on …

select a.s_name,b.s_score from student a right join score b on a.s_id = b.s_id;

The second way of writing: right outer join … on …

select a.s_name,b.s_score from student a right outer join score b on a.s_id = b.s_id;

insert image description here

Subqueries

Subquery : It is a way to implement multi-table join query. Another select statement is nested in the from clause or where clause of a select statement. The outer select query statement becomes the main query. In other words , the query statement in WHERE or FORM is called a subquery.

Subquery in WHERE clause: The value returned by the subquery is used as the query condition of the main query

Subquery in the FROM clause: The subquery returns a virtual table, and the main query finds the conditions that meet the conditions from the temporary table.

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

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

<<:  Linux operation and maintenance basics httpd static web page tutorial

>>:  How to use CSS3 to implement a queue animation similar to online live broadcast

Recommend

How to manage users and groups when running Docker

Docker is a management tool that uses processes a...

How to View All Running Processes in Linux

You can use the ps command. It can display releva...

Mount the disk in a directory under Ubuntu 18.04

Introduction This article records how to mount a ...

el-table in vue realizes automatic ceiling effect (supports fixed)

Table of contents Preface Implementation ideas Ef...

Detailed explanation of single-row function code of date type in MySQL

Date-type single-row functions in MySQL: CURDATE(...

vue.config.js packaging optimization configuration

The information on Baidu is so diverse that it...

Installation steps of mysql under linux

1. Download the mysql tar file: https://dev.mysql...

Detailed explanation of the use of various MySQL indexes

1. Slow query log 1.1 MySQL log types Logs are us...

ReactRouter implementation

ReactRouter implementation ReactRouter is the cor...

Handwritten Vue2.0 data hijacking example

Table of contents 1: Build webpack 2. Data hijack...

The vue project realizes drawing a watermark in a certain area

This article shares with you how to use Vue to dr...

JS quickly master ES6 class usage

1. How to construct? Let's review the common ...

Detailed explanation of the two modes of Router routing in Vue: hash and history

hash mode (default) Working principle: Monitor th...

Detailed explanation of the method of comparing dates in MySQL

If there is a table product with a field add_time...