Oracle 连接 JOIN、LEFT JOIN、RIGHT JOIN、INNER JOIN、OUTER JOIN

Understanding and usage of Oracle connection JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.

First understand what the data will look like after the connection:

 Simulate the data situation:

CREATE TABLE FRUIT_COUNTER (
    NAME VARCHAR2(20),
    PRICE NUMBER
);
COMMENT ON TABLE FRUIT_COUNTER IS '水果-柜台售卖';
COMMENT ON COLUMN FRUIT_COUNTER.NAME IS '水果-名称';
COMMENT ON COLUMN FRUIT_COUNTER.PRICE IS '水果-价格';

INSERT INTO FRUIT_COUNTER (NAME, PRICE) VALUES ('苹果', 8);
INSERT INTO FRUIT_COUNTER (NAME, PRICE) VALUES ('草莓', 15);
INSERT INTO FRUIT_COUNTER (NAME, PRICE) VALUES ('樱桃', 45);
COMMIT;

CREATE TABLE FRUIT_REPERTORY (
    NAME VARCHAR2(20),
    REPERTORY NUMBER
);
COMMENT ON TABLE FRUIT_REPERTORY IS '水果库存';
COMMENT ON COLUMN FRUIT_REPERTORY.NAME IS '水果-名称';
COMMENT ON COLUMN FRUIT_REPERTORY.REPERTORY IS '水果-库存数量';

INSERT INTO FRUIT_REPERTORY (NAME, REPERTORY) VALUES ('苹果', 200);
INSERT INTO FRUIT_REPERTORY (NAME, REPERTORY) VALUES ('青提', 300);
INSERT INTO FRUIT_REPERTORY (NAME, REPERTORY) VALUES ('樱桃', 100);
COMMIT;

1. JOIN / INNER JOIN

The SQL JOIN clause is used to join rows from two or more tables based on common fields between those tables.

The most common type of JOIN: SQL INNER JOIN (simple JOIN) . SQL INNER JOIN returns all rows from multiple tables that satisfy the JOIN condition.

grammar:

SELECT column1, column2, ...
FROM table1
JOIN table2 ON condition;

Parameter Description:

  • column1, column2, ... : The names of the fields to be selected, which can be multiple fields. If no field names are specified, all fields will be selected.
  • table1 : The first table to join.
  • table2 : The second table to join.
  • condition : Connection condition, used to specify the connection method.

 Example:

Get the data of the same fruit in the two tables of 'counter' and 'warehouse'

SELECT *
FROM FRUIT_COUNTER C
JOIN FRUIT_REPERTORY R ON C.NAME = R.NAME ;

 Data that appears in both the counter and warehouse tables

 

 

 

 2. LEFT JOIN

The LEFT JOIN keyword returns all rows from the left table (table1), even if there are no matches in the right table (table2). If there is no match in the right table, the result is NULL.

SQL LEFT JOIN syntax

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

Note: In some databases, LEFT JOIN is called LEFT OUTER JOIN.

 Example:

Get the fruit on the counter in stock in the warehouse 

SELECT *
FROM FRUIT_COUNTER C
LEFT JOIN FRUIT_REPERTORY R ON C.NAME = R.NAME ;

 

  三、LEFT JOIN

The RIGHT JOIN keyword returns all rows from the right table (table2), even if there are no matches in the left table (table1). If there is no match in the left table, the result is NULL.

SQL RIGHT JOIN syntax

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN.

Example: 

Get the price of fruit in stock on the counter

SELECT *
FROM FRUIT_COUNTER C
RIGHT JOIN FRUIT_REPERTORY R ON C.NAME = R.NAME ;

   Fourth, FULL JOIN

The FULL OUTER JOIN keyword returns rows as long as there is a match in one of the left table (table1) and right table (table2).

The FULL OUTER JOIN keyword combines the results of LEFT JOIN and RIGHT JOIN.

SQL FULL OUTER JOIN syntax

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

Example: 

Get the stock of fruit at the counter, get the price of the fruit in stock

SELECT *
FROM FRUIT_COUNTER C
FULL JOIN FRUIT_REPERTORY R ON C.NAME = R.NAME ;

 

 Summarize:

The above are several ways to connect the database through JOIN. You can do several exercises in the database to facilitate intuitive understanding.

Usage of Oracle function function

Guess you like

Origin blog.csdn.net/qq_57226198/article/details/129839023