在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)庫(kù)/ MariaDB右外連接
MariaDB比較運(yùn)算符
MariaDB查詢數(shù)據(jù)
MariaDB Like子句
MariaDB Sum()函數(shù)
MariaDB過(guò)程
MariaDB限制返回記錄
MariaDB Count()函數(shù)
MariaDB更新數(shù)據(jù)
MariaDB導(dǎo)出數(shù)據(jù)
MariaDB Intersect運(yùn)算符
MariaDB函數(shù)
MariaDB Min()函數(shù)
MariaDB創(chuàng)建數(shù)據(jù)庫(kù)
MariaDB Avg()函數(shù)
MariaDB刪除數(shù)據(jù)
MariaDB條件
MariaDB功能特點(diǎn)
MariaDB創(chuàng)建表
MariaDB左外連接
MariaDB Union運(yùn)算符
MariaDB安裝
MariaDB選擇數(shù)據(jù)庫(kù)
MariaDB Max()函數(shù)
MariaDB Where子句
MariaDB右外連接
MariaDB內(nèi)連接
MariaDB截?cái)啾?/span>
MariaDB Order By子句
MariaDB教程
MariaDB正則表達(dá)式
MariaDB Union All運(yùn)算符
MariaDB Distinct子句
MariaDB修改表
MariaDB刪除表
MariaDB插入數(shù)據(jù)
MariaDB From子句
MariaDB刪除數(shù)據(jù)庫(kù)
MariaDB簡(jiǎn)介
MariaDB數(shù)據(jù)類(lèi)型

MariaDB右外連接

MariaDB RIGHT OUTER JOIN用于返回ON條件中指定的右表中的所有行,并且僅返回來(lái)自其他表中連接字段滿足條件的行。

MariaDB RIGHT OUTER JOIN也被稱為RIGHT JOIN。

語(yǔ)法:

SELECT columns  
FROM table1  
RIGHT [OUTER] JOIN table2  
ON table1.column = table2.column;

圖形表示如下:

注: 上圖中,兩個(gè)圖形的左側(cè)表(table1)和右側(cè)表(table2)中間交叉藍(lán)色部分,以及右側(cè)表(table2)就是連接返回的結(jié)果集。

為了方便演示,我們需要?jiǎng)?chuàng)建兩個(gè)表,并插入一些數(shù)據(jù) -

USE testdb;
DROP table if exists students;
DROP table if exists subjects;
DROP table if exists scores;
-- 學(xué)生信息
CREATE TABLE students(  
    student_id INT NOT NULL AUTO_INCREMENT,  
    student_name VARCHAR(100) NOT NULL,  
    student_address VARCHAR(40) NOT NULL,  
    admission_date DATE,  
    PRIMARY KEY ( student_id )
);

-- 科目信息
CREATE TABLE subjects(  
    subject_id INT NOT NULL AUTO_INCREMENT,  
    subject_name VARCHAR(100) NOT NULL,
    PRIMARY KEY ( subject_id )
);

-- 成績(jī)信息
CREATE TABLE scores(  
    id INT NOT NULL AUTO_INCREMENT,
    student_id int(10) NOT NULL,
    subject_id int(10) NOT NULL,
    score float(4,1) DEFAULT NULL,
    created_time datetime DEFAULT NULL,
    PRIMARY KEY ( id )
);

插入數(shù)據(jù) -

--- 學(xué)生信息數(shù)據(jù)
INSERT INTO students  
(student_id, student_name, student_address, admission_date)  
VALUES(1,'Maxsu','Haikou','2017-01-07 00:00:00');
INSERT INTO students  
(student_id, student_name, student_address, admission_date)  
VALUES  
(2,'JMaster','Beijing','2016-05-07 00:00:00'),  
(3,'Mahesh','Guangzhou','2016-06-07 00:00:00'),  
(4,'Kobe','Shanghai','2016-02-07 00:00:00'),  
(5,'Blaba','Shenzhen','2016-08-07 00:00:00');

-- 科目信息數(shù)據(jù)
INSERT INTO subjects  
(subject_id, subject_name)  
VALUES(1,'計(jì)算機(jī)網(wǎng)絡(luò)基礎(chǔ)');

INSERT INTO subjects  
(subject_id, subject_name)  
VALUES(2,'高等數(shù)學(xué)');

INSERT INTO subjects  
(subject_id, subject_name)  
VALUES(3,'離散數(shù)學(xué)');

-- 分?jǐn)?shù)
INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(1,1,81,'2017-11-18 19:30:02');

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(1,2,89,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(1,3,92,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(2,2,95,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(2,3,72,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(3,1,59,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(3,3,77,NOW());

INSERT INTO scores  
(student_id, subject_id, score, created_time)  
VALUES(4,2,81,NOW());

當(dāng)前studens表中的行記錄如下 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          2 | JMaster      | Beijing         | 2016-05-07     |
|          3 | Mahesh       | Guangzhou       | 2016-06-07     |
|          4 | Kobe         | Shanghai        | 2016-02-07     |
|          5 | Blaba        | Shenzhen        | 2016-08-07     |
+------------+--------------+-----------------+----------------+
5 rows in set (0.00 sec)

當(dāng)前score表中的行記錄如下 -

MariaDB [testdb]> select * from scores;
+----+------------+------------+-------+---------------------+
| id | student_id | subject_id | score | created_time        |
+----+------------+------------+-------+---------------------+
|  1 |          1 |          1 |  81.0 | 2017-11-18 19:30:02 |
|  2 |          1 |          2 |  89.0 | 2017-11-28 22:31:57 |
|  3 |          1 |          3 |  92.0 | 2017-11-28 22:31:58 |
|  4 |          2 |          2 |  95.0 | 2017-11-28 22:31:58 |
|  5 |          2 |          3 |  72.0 | 2017-11-28 22:31:58 |
|  6 |          3 |          1 |  59.0 | 2017-11-28 22:31:58 |
|  7 |          3 |          3 |  77.0 | 2017-11-28 22:31:58 |
|  8 |          4 |          2 |  81.0 | 2017-11-28 22:31:58 |
+----+------------+------------+-------+---------------------+
8 rows in set (0.00 sec)

示例1

使用以下語(yǔ)法根據(jù)給定的條件連接兩個(gè)表 - studentsscores,即查詢學(xué)生信息和對(duì)應(yīng)的成績(jī)信息,如果沒(méi)有成績(jī)則使用NULL值表示。

SELECT scores.subject_id, scores.score,students.student_id, students.student_name
FROM scores
RIGHT JOIN students
ON students.student_id = scores.student_id
ORDER BY students.student_id;

上面查詢語(yǔ)句查詢所有科目的考試分?jǐn)?shù)以及學(xué)生,得到以下結(jié)果 -

MariaDB [testdb]> SELECT scores.subject_id, scores.score,students.student_id, students.student_name
    -> FROM scores
    -> RIGHT JOIN students
    -> ON students.student_id = scores.student_id
    -> ORDER BY students.student_id;
+------------+-------+------------+--------------+
| subject_id | score | student_id | student_name |
+------------+-------+------------+--------------+
|          1 |  81.0 |          1 | Maxsu        |
|          2 |  89.0 |          1 | Maxsu        |
|          3 |  92.0 |          1 | Maxsu        |
|          2 |  95.0 |          2 | JMaster      |
|          3 |  72.0 |          2 | JMaster      |
|          1 |  59.0 |          3 | Mahesh       |
|          3 |  77.0 |          3 | Mahesh       |
|          2 |  81.0 |          4 | Kobe         |
|       NULL |  NULL |          5 | Blaba        |
+------------+-------+------------+--------------+
9 rows in set (0.00 sec)

上面示例的查詢結(jié)果中,由于最后一行(student_id=5)的學(xué)生還沒(méi)有任何分?jǐn)?shù)信息,所以在使用RIGHT JOIN連接后,左側(cè)表(scores)相關(guān)列的值使用NULL來(lái)填充。可以看到右側(cè)表(students)的每一行都有列出來(lái)了。

示例2

查詢指定學(xué)生,并且成績(jī)大于85分的信息 -

SELECT students.student_id, students.student_name, scores.subject_id, scores.score  
FROM scores
RIGHT JOIN students
ON students.student_id = scores.student_id
WHERE students.student_name='Maxsu' AND scores.score > 85;

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT students.student_id, students.student_name, scores.subject_id, scores.score
    -> FROM scores
    -> RIGHT JOIN students
    -> ON students.student_id = scores.student_id
    -> WHERE students.student_name='Maxsu' AND scores.score > 85;
+------------+--------------+------------+-------+
| student_id | student_name | subject_id | score |
+------------+--------------+------------+-------+
|          1 | Maxsu        |          2 |  89.0 |
|          1 | Maxsu        |          3 |  92.0 |
+------------+--------------+------------+-------+
2 rows in set (0.00 sec)

示例3

查詢沒(méi)有考試成績(jī)的學(xué)生信息(尚未錄入) -

SELECT students.student_id, students.student_name, scores.subject_id, scores.score  
FROM scores
RIGHT JOIN students
ON students.student_id = scores.student_id
WHERE scores.score IS NULL;

執(zhí)行上面查詢語(yǔ)句,得到以下結(jié)果 -

MariaDB [testdb]> SELECT students.student_id, students.student_name, scores.subject_id, scores.score
    -> FROM scores
    -> RIGHT JOIN students
    -> ON students.student_id = scores.student_id
    -> WHERE scores.score IS NULL;
+------------+--------------+------------+-------+
| student_id | student_name | subject_id | score |
+------------+--------------+------------+-------+
|          5 | Blaba        |       NULL |  NULL |
+------------+--------------+------------+-------+
1 row in set (0.00 sec)