Mysql 简明教程
MySQL - Right Join
MySQL Right Join
Right Join 或 Right Outer Join 查询在 MySQL 中返回右表中的所有行,即使左表中没有匹配项。因此,如果左表中没有匹配到记录,右联接仍将在结果中返回一行,但左表中每一列的值都为 NULL。
The Right Join or Right Outer Join query in MySQL returns all rows from the right table, even if there are no matches in the left table. So, if zero records are matched in the left table, the right join will still return a row in the result, but with a NULL value in each column of the left table.
简而言之,右联接返回右表中的所有值,以及左表中的匹配值或在没有匹配联接谓词的情况下为 NULL。
In short, a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate.
在实现右联接后显示的结果表并未存储在数据库中的任何位置。
The resultant table displayed after implementing the Right Join is not stored anywhere in the database.
Syntax
以下是 SQL 中右侧连接的基本语法:
Following is the basic syntax of Right Join in SQL −
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
Example
假设我们正在创建一个名为 CUSTOMERS 的表格,其中包含客户的个人详细信息,包括他们的姓名、年龄、地址和工资等。
Assume we are creating a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc.
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
现在使用 INSERT 语句向该表中插入值,如下所示:
Now insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );
该表将被创建为:
The table will be created as −
让我们创建另一个名为 ORDERS 的表,其中包含所做的订单和下单日期的详细信息。
Let us create another table ORDERS, containing the details of orders made and the date they are made on.
CREATE TABLE ORDERS (
OID INT NOT NULL,
DATE VARCHAR (20) NOT NULL,
CUSTOMER_ID INT NOT NULL,
AMOUNT DECIMAL (18, 2),
);
使用 INSERT 语句像下面这样向该表中插入值:
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);
该表显示如下:
The table is displayed as follows −
Right join Query
Right join Query
现在,让我们使用如下右联接查询联接这两个表。
Now, let us join these two tables using the Right Join query as follows.
SELECT ID, NAME, AMOUNT, DATE
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Joining Multiple Tables with Right Join
与左侧连接类似,右侧连接也会连接多个表格。然而,区别在于右侧表格会整体返回,而不是左侧表格。
Like Left Join, Right Join also joins multiple tables. However, the contrast occurs where the second table is returned as a whole instead of the first.
Syntax
以下是使用右侧连接来连接多个表格的语法:
Following is the syntax to join multiple tables using Right Join −
SELECT column1, column2, column3...
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name
RIGHT JOIN table3
ON table2.column_name = table3.column_name
.
.
.
Example
在此,让我们考虑先前创建的表 CUSTOMERS 和 ORDERS;连同新创建的表 EMPLOYEE。
Here, let us consider the previously created tables CUSTOMERS and ORDERS; along with the newly created table EMPLOYEE.
我们将使用以下查询创建 EMPLOYEE 表 −
We will create the EMPLOYEE table using the query below −
CREATE TABLE EMPLOYEE (
EID INT NOT NULL,
EMPLOYEE_NAME VARCHAR (30) NOT NULL,
SALES_MADE DECIMAL (20)
);
现在,我们可以使用 INSERT 语句将值插入到此空表中,如下所示:
Now, we can insert values into this empty tables using the INSERT statement as follows −
INSERT INTO EMPLOYEE VALUES
(102, 'SARIKA', 4500),
(100, 'ALEKHYA', 3623),
(101, 'REVATHI', 1291),
(103, 'VIVEK', 3426);
表创建如下 −
The table is created as −
让我们使用 Right Join query 将这三个表联接起来 −
Let us join these three tables using the Right Join query given below −
SELECT CUSTOMERS.ID, CUSTOMERS.NAME, ORDERS.DATE, EMPLOYEE.EMPLOYEE_NAME
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
RIGHT JOIN EMPLOYEE
ON ORDERS.OID = EMPLOYEE.EID;
通过此查询,我们尝试显示客户 ID、客户姓名、特定日期下单以及销售它们的员工姓名。
Through this query, we are trying to display the records of Customer IDs, Customer names, Orders made on specific dates and names of the employees that sold them.
Right Join with WHERE Clause
WHERE 子句用于筛选满足它指定条件的记录。此子句可以与右联接技术结合使用,以对获得的结果集应用约束。
A WHERE Clause is used to filter out records that satisfy the condition specified by it. This clause can be used with the Right Join technique to apply constraints on the result-set obtained.
Syntax
使用 WHERE 子句时的右侧连接语法如下:
The syntax of Right Join when used with WHERE clause is given below −
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name = table_name2.column_name
WHERE condition
Example
可以使用 WHERE 子句来筛选合并数据库表中的记录。考虑之前的两个表 CUSTOMERS 和 ORDERS;并使用以下查询将它们联接起来 −
Records in the combined database tables can be filtered using the WHERE clause. Consider the previous two tables CUSTOMERS and ORDERS; and join them using the following query −
SELECT ID, NAME, DATE, AMOUNT FROM CUSTOMERS
RIGHT JOIN ORDERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
WHERE ORDERS.AMOUNT > 1000.00;