Home Database Mysql Tutorial sql json brief introduction

sql json brief introduction

Nov 24, 2016 am 11:26 AM
json mysql sql

Preface

This article still adheres to the previous writing style, using simple and easy-to-understand examples to help everyone understand the differences between various joins.

 Why do we need to join

 Why do we need to join? Join in Chinese means connection, and connection means association, which is to associate one table with multiple tables. When dealing with database tables, we often find that we need to obtain information from multiple tables, assemble multiple field data from multiple tables, and then return it to the caller. Therefore, the premise of join is that there must be related fields between these tables.

Classification of join

Join is divided into two types, inner join and outer join. Outer join is divided into three types, left outer join, right outer join, full outer join. In addition, left outer join is also referred to as left join for short. That is, the well-known left join.

sql json brief introduction

Join is divided into two types, inner join and outer join. Outer join is divided into three types, left outer join, right outer join, full outer join. In addition, left outer join is also known as left join for short. left join.

The differences between various joins

Before introducing the differences between various joins, let’s take a look at a simple example:

Scene description:

In the Internet era, everyone likes to shop online, especially Taobao and JD.com. So the scene we chose is also familiar to everyone in online shopping. This is a story about a person and what products he bought in the mall;

In response to the above requirements, we created two tables, tb_person and tb_order, where tb_person is a description of this person, and tb_order is about his purchase A description of the product.

Our table structure is very simple. tb_person only needs to know who the person is, so there are only three fields id, firstname (first name) and lastname (surname). Similarly, tb_order is also very simple. We only need to know who bought it. What product, so only 3 fields are needed, namely oid, oname (product name), pid (buyer number).

 tb_person:

+-----------+-------------+------+-----+---- -----+----------------+

| Field | Type | Null | Key | Default | Extra |

+--------- --+-------------+------+-----+---------+---------- ------+

| pid | int(11) | NO | PRI | NULL | auto_increment |

| firstname | varchar(50) | YES | | NULL |

| lastname | varchar(50) | YES | | NULL | --------+----------------+

tb_order:

+------+--------- ----+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+-------+------+-- -------+----------------+

| oid | int(11) | NO | PRI | NULL | auto_increment |

| oname | varchar(50) | YES | | int(11) | YES | | NULL |

+-------+-------------+------+-----+---------+--- -------------+

Next, we write some sample data to the above two tables:

 data in tb_person:

+-----+----- ------+----------+

| pid | firstname | lastname |

+-----+-----------+-- --------+

| 1 | andy | chen |

| 2 | irri | wan |

| 3 | abby | sun |

+-----+----- ------+----------+

There are three people in the tb_person table, namely andy Chen, irri Wan, abby Sun;

  data in tb_order:

+--- --+----------+------+

| oid | oname | pid |

+-----+----------+ ------+

| 1 | book | 1 |

| 2 | phone | 1 |

| 3 | computer | 4 |

+-----+------- ---+------+

tb_order table records 3 pieces of data, the person number is 1, that is, Andy Chen bought two items, namely book and phone, and there is another person number 4. A person bought a product, computer. You may have questions about this, why is there no person with personnel number 4 in the tb_person table? Here we assume that because there are many registered users, we have adopted the user table strategy, so the user with personnel number 4 may be in another personnel table.

 From the previous description, we know that if you want to join between tables, there must be an associated field. In the above example, we see that the associated field is pid.

 According to the two tables tb_person and tb_order, we can see three situations:

The person in the person table purchased the product, that is, there is a record of the user’s product purchase in the order table, and we can query it from this table Find out which products the user has purchased. For example, Andy Chen purchased two products: book and phone. That is, the pid exists in both the tb_person and tb_order tables. People in the

person table have not purchased the products, such as irri Wan and abby Sun. The two users did not purchase any goods, that is, the pid only exists in the tb_person table;

The user who purchased the goods in the order table cannot find a record in the person table. For example, the user with pid 4 purchased a computer but in the tb_person table There is no record of the user in the tb_order table, that is, the pid only exists in the tb_order table;

Understanding the above three situations is very helpful for us to understand the join. Next, we will analyze the differences of each join in detail:

INNER JOIN

 The so-called inner join means the situation 1 we mentioned earlier, pid must exist in both tables tb_person and tb_order;

MariaDB [demo]> SELECT p.pid, p.firstname, o.oname
    -> FROM tb_person p
    -> INNER JOIN tb_order o
    -> ON p.pid=o.pid;
Copy after login

+-----+-----------+-- -----+

| pid | firstname | oname |

+-----+-----------+-------+

| 1 | andy | book |

| 1 | andy | phone |

+-----+----------+-------+

LEFT JOIN

 tb_person LEFT JOIN tb_order means the union of case 1 and case 2 above. The result set of LEFT JOIN not only contains the results of INNER JOIN, but also contains the set of all users in tb_person who did not purchase any goods.

MariaDB [demo]> SELECT p.pid, p.firstname, o.oname
    -> FROM tb_person p
    -> LEFT JOIN tb_order o
    -> ON p.pid=o.pid;
Copy after login

+-----+-----------+-------+

| pid | firstname | oname |

+-----+-- ---------+-------+

| 1 | andy | book |

| 1 | andy | phone |

| 2 | irri | NULL |

| 3 | abby | NULL |

+-----+-----------+-------+

 RIGHT JOIN

  tb_person RIGHT JOIN tb_order的意思是上述情形1和情形3的并集。RIGHT JOIN的结果集不仅包含INNER JOIN的结果,而且还包含所有tb_order中所有已经购买商品的用户但该用户记录不存在于tb_person表。

MariaDB [demo]> SELECT p.pid, p.firstname, o.oname
    -> FROM tb_person p
    -> RIGHT JOIN tb_order o
    -> ON p.pid=o.pid;
Copy after login

+------+-----------+----------+

| pid | firstname | oname |

+------+-----------+----------+

| 1 | andy | book |

| 1 | andy | phone |

| NULL | NULL | computer |

+------+-----------+----------+

FULL JOIN

  故名思议,FULL JOIN就是上述情形1,2,3的并集了,但是mysql数据库不支持full join查询,所以我们只能LEFT JOIN union RIGHT JOIN,才能得到FULL JOIN的结果。

MariaDB [demo]> SELECT p.pid, p.firstname, o.oname
    -> FROM tb_person p
    -> LEFT JOIN tb_order o
    -> ON p.pid=o.pid
    -> UNION
    -> SELECT p.pid, p.firstname, o.oname
    -> FROM tb_person p
    -> RIGHT JOIN tb_order o
    -> ON p.pid=o.pid;
Copy after login

+------+-----------+----------+

| pid | firstname | oname |

+------+-----------+----------+

| 1 | andy | book |

| 1 | andy | phone |

| 2 | irri | NULL |

| 3 | abby | NULL |

| NULL | NULL | computer |

+------+-----------+----------+

 注:我们上述的sql语句全部基于mysql数据库执行。

 总结

  本文主要描述了sql join的分类以及各种join的区别,通过简单的示例,让大家更清晰的去了解他们。至于什么时候使用join要视具体的情况而定,根据不同的需求采用不同的策略。

  非常感谢大家的热心回复,可能有些问题的探讨超出了本文的范畴,但是非常乐意大家提出问题,然后大家一起去探索去发现。

 引用

  NULL

 附件

 demo.sql文件

create database demo;
use demo;
 
create table tb_person (
    pid int(11) auto_increment,
    firstname varchar(50),
    lastname varchar(50),
    primary key(pid)
);
 
create table tb_order (
    oid int(11) auto_increment,
    oname varchar(50),
    pid int(11),
    primary key(oid)
);
 
insert into tb_person(firstname, lastname) values('andy','chen');
insert into tb_person(firstname, lastname) values('irri','wan');
insert into tb_person(firstname, lastname) values('abby','sun');
 
insert into tb_order(oname, pid) values('book', 1);
insert into tb_order(oname, pid) values('phone', 1);
insert into tb_order(oname, pid) values('computer', 4);
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL vs. Other Databases: Comparing the Options MySQL vs. Other Databases: Comparing the Options Apr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

See all articles