[MySQL] Cross join, natural join and inner join query

1. Introduction

        In actual development, it is often necessary to operate on two or more data tables, and these multiple tables need to be associated with each other using primary keys and foreign keys , and then use join queries to query data records that meet the requirements in multiple tables . A single SQL statement queries multiple tables and obtains a result containing data from multiple tables. efficient.

Various types of join queries:

  • cross
  • natural
  • using
  • on

2. Cross join

        Cross connection (CROSS JOIN) is a Cartesian product operation on two or more tables. The so-called Cartesian product is a concept in relational algebra, which represents the result of any combination of data in each row of two tables. For example, if there are two tables, the left table has m data records, x fields, and the right table has n data records, y fields, then m*n data records, x+y fields will be returned after cross connection . The schematic diagram of the Cartesian product is shown in Fig.  

        I want to find the data of the four fields of employee number, employee name, department number, and department name. In the employee table emp, the data of employee number, employee name, and department number can be queried. In the department table dept, the field department can be queried. Name data, if you want to find these data at the same time, you need to use multi-table query syntax, cross join cross join:

Query employee table emp:

select * from emp;

 

Query the department table dept:

select * from dept;

One SQL query two tables:

select * from emp cross join dept;

There are 14 records in table emp and 4 records in table dept. After cross-join query, 14*4=56 records. Cross-join is to perform Cartesian product operation on these two tables. Cartesian product has no practical significance, but it has theoretical significance.

Regarding the writing method of cross connection, cross can be omitted in MySQL, but cannot be omitted in Oracle. The above SQL is equivalent to:

select * from emp join dept;

3. Natural join

The cross-connect will query a lot of redundant data. For example, in the employee table emp and the department table dept, the foreign key deptno that associates the primary key with the foreign key does not match together, resulting in data redundancy:

Using natural joins, you can automatically match all columns with the same name, so that the columns with the same name are only displayed once in the query, improving query efficiency.

select * from emp natural join dept;

Some fields of the query can be specified:

select empno,ename,deptno,dname from emp natural join dept;

When querying a field, the system will search from the two associated tables separately, so the efficiency is low. To solve this problem, we can specify the table name when querying the target field in the format of table name. field name

select emp.empno,emp.ename,emp.deptno,dept.dname from emp natural join dept;

The query results are consistent with the above, but the query efficiency has been improved.

At this time, if the table name is too long, the query SQL will also be too long. When querying, we can alias the table:

select e.empno,e.ename,d.dname,d.deptno
from emp e
natural join dept d;

4. Inner connection

The disadvantage of using natural join: it will automatically match all the columns with the same name in the table, but sometimes we want to only match some columns with the same name, then we can use the using clause, which is an inner join (inner join)

select * 
from emp as e
inner join dept as d
using(deptno);

Disadvantages of the using clause: the associated fields must have the same name Solution: use the on clause
in the inner connection

select * 
from emp e
inner join dept d
on (e.deptno = d.deptno);

 

V. Summary

The types of multi-table queries are:

  1. cross join
  2. Natural join natural join
  3. Inner join - using clause
  4. inner join - on clause

Taken together: Inner joins - the on clause is used most frequently.
 

6. Supplement

select *
from emp e
inner join dept d
on (e.deptno = d.deptno)
where sal > 3500;

Conditions:
1. Filter condition where having
2. Join condition on/using/natural
In SQL99 syntax, filter condition and join condition are separated.

Guess you like

Origin blog.csdn.net/hold_on_qlc/article/details/129500899