MySQL - RIGHT JOIN 右连接
MySQL 右连接
MySQL 中的右连接或右外连接查询会返回右表中的所有行,即使左表中没有匹配项。因此,如果左表中没有匹配的记录,右连接仍会在结果中返回一行,但左表的每一列都会返回 NULL 值。
简而言之,右连接会返回右表的所有值,以及左表中匹配的值;如果没有匹配的连接谓词,则返回 NULL。

执行右连接后显示的结果表不会存储在数据库的任何位置。
语法
以下是 SQL 中右连接的基本语法 -
SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_field = table2.common_field;
示例
假设我们正在创建一个名为 CUSTOMERS 的表,其中包含客户的个人信息,包括姓名、年龄、地址和工资等。
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 语句将值插入到该表中,如下所示 -
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 );
该表将创建为 −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
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 |
让我们创建另一个表 ORDERS,其中包含已下订单的详细信息及其下单日期。
CREATE TABLE ORDERS ( OID INT NOT NULL, DATE VARCHAR (20) NOT NULL, CUSTOMER_ID INT NOT NULL, AMOUNT DECIMAL (18, 2), );
使用 INSERT 语句,将值插入此表,如下所示 -
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);
表显示如下 −
OID | DATE | CUSTOMER_ID | AMOUNT |
---|---|---|---|
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 |
右连接查询
现在,让我们使用右连接查询连接这两个表,如下所示。
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
输出
这将产生以下结果 -
ID | NAME | AMOUNT | DATE |
---|---|---|---|
3 | Kaushik | 3000.00 | 2009-10-08 00:00:00 |
3 | Kaushik | 1500.00 | 2009-10-08 00:00:00 |
2 | Khilan | 1560.00 | 2009-11-20 00:00:00 |
4 | Chaitali | 2060.00 | 2008-05-20 00:00:00 |
使用右连接连接多个表
与左连接类似,右连接也可以连接多个表。然而,不同的是,右连接返回的是整个第二个表,而不是第一个表。
语法
以下是使用右连接连接多个表的语法 -
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 . . .
示例
这里,我们考虑之前创建的表 CUSTOMERS 和 ORDERS;以及新创建的表 EMPLOYEE。
我们将使用以下查询创建 EMPLOYEE 表 -
CREATE TABLE EMPLOYEE ( EID INT NOT NULL, EMPLOYEE_NAME VARCHAR (30) NOT NULL, SALES_MADE DECIMAL (20) );
现在,我们可以使用 INSERT 语句将值插入到这个空表中,如下所示 -
INSERT INTO EMPLOYEE VALUES (102, 'SARIKA', 4500), (100, 'ALEKHYA', 3623), (101, 'REVATHI', 1291), (103, 'VIVEK', 3426);
该表创建为 −
EID | EMPLOYEE_NAME | SALES_MADE |
---|---|---|
102 | SARIKA | 4500 |
100 | ALEKHYA | 3623 |
101 | REVATHI | 1291 |
103 | VIVEK | 3426 |
让我们使用下面给出的右连接查询来连接这三个表 -
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、客户名称、特定日期下的订单以及销售这些订单的员工姓名的记录。
输出
结果表如下所示 -
ID | NAME | DATE | EMPLOYEE_NAME |
---|---|---|---|
3 | Kaushik | 2009-10-08 00:00:00 | SARIKA |
3 | Kaushik | 2009-10-08 00:00:00 | ALEKHYA |
2 | Khilan | 2009-11-20 00:00:00 | REVATHI |
4 | Chaitali | 2008-05-20 00:00:00 | VIVEK |
使用 WHERE 子句的右连接
WHERE 子句用于筛选出满足其指定条件的记录。此子句可与右连接技术一起使用,对获得的结果集应用约束。
语法
右连接与 WHERE 子句一起使用时的语法如下 -
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name = table_name2.column_name WHERE condition
示例
可以使用 WHERE 子句筛选合并后的数据库表中的记录。考虑前两个表 CUSTOMERS 和 ORDERS;并使用以下查询将它们连接起来 -
SELECT ID, NAME, DATE, AMOUNT FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID WHERE ORDERS.AMOUNT > 1000.00;
输出
结果表如下 -
ID | NAME | DATE | Amount |
---|---|---|---|
3 | Kaushik | 2009-10-08 00:00:00 | 3000.00 |
3 | Kaushik | 2009-10-08 00:00:00 | 1500.00 |
2 | Khilan | 2009-11-20 00:00:00 | 1560.00 |
4 | Chaitali | 2008-05-20 00:00:00 | 2060.00 |
使用客户端程序进行右连接
我们也可以使用客户端程序对一个或多个表执行右连接操作。
语法
要在 PHP 程序中使用右连接来连接两个表,我们需要使用 mysqli 函数 query() 执行带有 RIGHT JOIN 子句的 SQL 查询,如下所示 -
$sql = 'SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author'; $mysqli->query($sql);
要通过 JavaScript 程序使用右连接来连接两个表,我们需要使用 mysql2 库的 query() 函数执行带有 RIGHT JOIN 子句的 SQL 查询,如下所示 -
sql = "SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author"; con.query(sql);
要在 Java 程序中使用右连接来连接两个表,我们需要使用 JDBC 函数 executeQuery() 执行带有 RIGHT JOIN 子句的 SQL 查询,如下所示 -
String sql = "SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author"; st.executeQuery(sql);
要通过 Python 程序使用右连接连接两个表,我们需要使用 MySQL Connector/Python 的 execute() 函数执行带有 RIGHT JOIN 子句的 SQL 查询,如下所示 -
right_join_query = "SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUST_ID" cursorObj.execute(right_join_query)
示例
以下是程序 -
$dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'password'; $dbname = 'TUTORIALS'; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); if ($mysqli->connect_errno) { printf("Connect failed: %s
", $mysqli->connect_error); exit(); } // printf('Connected successfully.
'); $sql = 'SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author'; $result = $mysqli->query($sql); if ($result->num_rows > 0) { echo " following is the both table details after executing right join! "; while ($row = $result->fetch_assoc()) { printf( "Id: %s, Author: %s, Count: %d", $row["tutorial_id"], $row["tutorial_author"], $row["tutorial_count"] ); printf(" "); } } else { printf('No record found.
'); } mysqli_free_result($result); $mysqli->close();
输出
获得的输出如下 -
following is the both table details after executing right join! Id: , Author: , Count: 20 Id: , Author: , Count: 5 Id: , Author: , Count: 4 Id: , Author: , Count: 20 Id: , Author: , Count: 1 Id: 3, Author: Sanjay, Count: 1
var mysql = require("mysql2"); var con = mysql.createConnection({ host: "localhost", user: "root", password: "password", }); //连接到 MySQL con.connect(function (err) { if (err) throw err; // console.log("Connected successfully...!"); // console.log("--------------------------"); sql = "USE TUTORIALS"; con.query(sql); //Right Join sql = "SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author"; con.query(sql, function (err, result) { if (err) throw err; console.log(result); }); });
输出
生成的输出如下 -
[ { tutorial_id: null, tutorial_author: null, tutorial_count: 20 }, { tutorial_id: null, tutorial_author: null, tutorial_count: 5 }, { tutorial_id: null, tutorial_author: null, tutorial_count: 4 }, { tutorial_id: null, tutorial_author: null, tutorial_count: 20 }, { tutorial_id: null, tutorial_author: null, tutorial_count: 1 }, { tutorial_id: null, tutorial_author: null, tutorial_count: 1 }, { tutorial_id: 1, tutorial_author: 'John Poul', tutorial_count: 2 } ]
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class RightJoin { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/TUTORIALS"; String username = "root"; String password = "password"; try { Class.forName("com.mysql.cj.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement(); System.out.println("Connected successfully...!"); //MySQL RIGHT JOIN...!; String sql = "SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count FROM tutorials_tbl a RIGHT JOIN tcount_tbl b ON a.tutorial_author = b.tutorial_author"; ResultSet resultSet = statement.executeQuery(sql); System.out.println("Table records after LEFT Join...!"); while (resultSet.next()){ System.out.println(resultSet.getString(1)+ " "+ resultSet.getString(2)+" "+resultSet.getString(3)); } connection.close(); } catch (Exception e) { System.out.println(e); } } }
输出
获得的输出如下所示 -
Connected successfully...! Table records after LEFT Join...! null null 20 null null 5 null null 4 null null 20 1 John Paul 1 3 Sanjay 1
import mysql.connector #建立连接 connection = mysql.connector.connect( host='localhost', user='root', password='password', database='tut' ) cursorObj = connection.cursor() right_join_query = f""" SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUST_ID """ cursorObj.execute(right_join_query) # 获取所有符合条件的行 filtered_rows = cursorObj.fetchall() for row in filtered_rows: print(row) cursorObj.close() connection.close()
输出
以下是上述代码的输出 -
(3, 'kaushik', 3000, '2009-10-08 00:00:00') (3, 'kaushik', 1500, '2009-10-08 00:00:00') (2, 'Khilan', 1560, '2009-11-20 00:00:00') (4, 'Chaital', 2060, '2008-05-20 00:00:00')