GaussDB database SQL series - table connection (JOIN)

Table of contents

I. Introduction

Second, GaussDB JOIN

1、LEFT JOIN

2、LEFT JOIN EXCLUDING INNER JOIN

3、RIGHT JOIN

4、LEFT JOIN EXCLUDING INNER JOIN

5、INNER JOIN

6、FULL OUTER JOIN

7、FULL OUTER JOIN EXCLUDING INNER JOIN

3. GaussDB experiment example

1. Initialize the experiment table

2. LEFT JOIN (example)

3. RIGTH JOIN (example)

4. INNER JOIN (example)

5. FULL JOIN (example)

Four. Summary

I. Introduction

SQL is one of the most important programming languages ​​for data analysis and data processing. Table join (JOIN) is a common operation of SQL in database. Get information from the table.

Second, GaussDB JOIN

GaussDB is an enterprise-level distributed relational database launched by Huawei. The GaussDB JOIN clause joins two or more tables based on common fields between them. In the GaussDB database, commonly used JOINs include the following connections and usages: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and CROSS JOIN.

1、LEFT JOIN

LEFT JOIN is generally called left connection, also written as LEFT [OUTER] JOIN. A left join query will return all the records in the left table, and the associated data columns found in the right table will also be returned together.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
LEFT JOIN table2 t2 
ON t1.id=t2.id ;

2、LEFT JOIN EXCLUDING INNER JOIN

Returns a recordset that has associated data in the left table but not in the right table.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
LEFT JOIN table2 t2 
ON t1.id=t2.id
WHERE t2.id IS NULL ;

3、RIGHT JOIN

RIGHT JOIN is generally called right connection, also written as RIGHT [OUTER] JOIN. A right join query will return all the records in the right table, and the associated data columns found in the left table will also be returned together.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
RIGHT JOIN table2 t2
ON t1.id=t2.id

4、LEFT JOIN EXCLUDING INNER JOIN

Returns a recordset that has associated data in the right table but not in the left table.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
RIGHT JOIN table2 t2
ON t1.id=t2.id
WHERE t1.id IS NULL ;

5、INNER JOIN

INNER JOIN is generally translated as an inner connection. Get the data that can be associated in the left table and the right table.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
INNER JOIN table2 t2
ON t1.id=t2.id ;

6、FULL OUTER JOIN

FULL [OUTER] JOIN is generally called outer connection and full connection, and it can be written as FULL JOIN in the actual query statement. Outer join queries can return all records in the left and right tables.

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
FULL OUTER JOIN table2 t2
ON t1.id=t2.id ;

7、FULL OUTER JOIN EXCLUDING INNER JOIN

Returns recordsets that are not related to each other in the left and right tables.

 

--SQL示例
SELECT  t1.column1
       ,…
       ,t2.column1 
	   ,…
FROM table1 t1 
FULL OUTER JOIN table2 t2
ON t1.id=t2.id 
WHERE t1.id IS NULL 
OR t2.id IS NULL ;

In addition to the above types, there is another CROSS JOIN (Cartesian set), but this usage is not commonly used, and further research can be done.

3. GaussDB experiment example

Create two experimental tables: Students (student table) and Score (student performance table).

1. Initialize the experiment table

1) Students (student table):

--学生表,Students(SNO, SNAME)代表 (学号,姓名)
DROP TABLE students;
CREATE TABLE students(
    sno    INTEGER     NOT NULL,
    sname  varchar(32)
);

--插入数据
INSERT INTO students(sno,sname) VALUES (1001,'张三');
INSERT INTO students(sno,sname) VALUES (1002,'李四');
INSERT INTO students(sno,sname) VALUES (1003,'王五');
INSERT INTO students(sno,sname) VALUES (1004,'赵六');
INSERT INTO students(sno,sname) VALUES (1005,'韩梅');
INSERT INTO students(sno,sname) VALUES (1006,'李雷');

--查看表信息
SELECT * FROM students;

 2) Score (student score sheet):

--学生成绩表,Score(SNO, SCGRADE) 代表(学号,成绩)
DROP TABLE score;
CREATE TABLE score(
    sno      INTEGER     NOT NULL,
    scgrade  DECIMAL(3,1)
);

--插入数据
INSERT INTO score(sno,scgrade)values(1001,98);
INSERT INTO score(sno,scgrade)values(1002,95);
INSERT INTO score(sno,scgrade)values(1003,97);	
INSERT INTO score(sno,scgrade)values(1004,99);

--查看表信息
SELECT * FROM score;

2. LEFT JOIN (example)

--表students为主表
SELECT t1.sno
      ,t1.sname
      ,t2.sno
      ,t2.scgrade
 FROM students t1
LEFT JOIN score t2
ON t1.sno=t2.sno

3. RIGTH JOIN (example)

--表score 为主表
SELECT t1.sno
      ,t1.sname
      ,t2.sno
      ,t2.scgrade
 FROM students t1
RIGHT JOIN score t2
ON t1.sno=t2.sno

4. INNER JOIN (example)

--根据字段sno获取两个表中都有的数据
SELECT t1.sno
      ,t1.sname
      ,t2.sno
      ,t2.scgrade
 FROM students t1
INNER JOIN score t2
ON t1.sno=t2.sno

5. FULL JOIN (example)

--获取左右表里的所有记录。
SELECT t1.sno
      ,t1.sname
      ,t2.sno
      ,t2.scgrade
 FROM students t1
FULL JOIN score t2
ON t1.sno=t2.sno

 

Four. Summary

Database table join (Join) is to combine the data in two or more tables according to certain conditions. In practical applications, database table join can help us quickly obtain the required data information and improve data processing efficiency. It should be noted that different database systems may have different support for table connection, and you need to choose an appropriate connection method according to the specific database type. (This article uses the GaussDB cloud database as the experimental platform)

--Finish

Guess you like

Origin blog.csdn.net/GaussDB/article/details/132180351