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

MariaDB Intersect運算符

INTERSECT運算符用于返回2個或更多表的交集。 如果兩個表中都存在記錄,它將被包含在INTERSECT結果中。 否則,它將從INTERSECT結果中被省略。

語法

SELECT expression1, expression2, ... expression_n  
FROM tables  
[WHERE conditions]  
INTERSECT  
SELECT expression1, expression2, ... expression_n  
FROM tables  
[WHERE conditions];

說明圖如下所示 -

注意:MariaDB不支持INTERSECT運算符,但是通過使用IN運算符來模擬INTERSECT查詢,可以看到相同的結果,如下示例中所示。

1. INTERSECT運算符返回單個字段

以下是針對INTERSECT運算符的一般查詢,但不會在MariaDB中超混雜。

假設有兩張表:studentsteachers。對應的表結構和數(shù)據(jù)如下 -

students表中的數(shù)據(jù):

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)

teachers表中的數(shù)據(jù):

USE testdb;
DROP TABLE teachers;
CREATE TABLE teachers(  
    teacher_id INT NOT NULL AUTO_INCREMENT,  
    name VARCHAR(100) NOT NULL,  
    address VARCHAR(40) NOT NULL,  
    admission_date DATE,  
    PRIMARY KEY ( teacher_id )
);
-- 插入數(shù)據(jù)
INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(1,'Maxsu','Haikou','2013-06-07 00:00:00');

INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(2,'張?zhí)旖?jīng)','廣州','2013-08-08 00:00:00');

INSERT INTO teachers  
(teacher_id, name, address, admission_date)  
VALUES(3,'李四光','三亞','2014-09-07 00:00:00');

經(jīng)過上創(chuàng)建和插入數(shù)據(jù),現(xiàn)在teachers表中擁有以下數(shù)據(jù)記錄 -

MariaDB [testdb]> select * from teachers;
+------------+--------+---------+----------------+
| teacher_id | name   | address | admission_date |
+------------+--------+---------+----------------+
|          1 | Maxsu  | Haikou  | 2013-06-07     |
|          2 | 張?zhí)旖?jīng) | 廣州    | 2013-08-08     |
|          3 | 李四光 | 三亞    | 2014-09-07     |
+------------+--------+---------+----------------+
3 rows in set (0.00 sec)

假設現(xiàn)在要查詢學生和老師的姓名有哪幾個?參考以下查詢語句 -

-- 理想語句,但是MariaDB中不支持 
SELECT student_name  
FROM Students
INTERSECT  
SELECT name  
FROM teachers;

上面語句的代替語句是 -

SELECT s.student_name AS name
FROM Students s
WHERE s.student_name IN (SELECT t.name FROM teachers t);

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

MariaDB [testdb]> SELECT s.student_name AS name
    -> FROM Students s
    -> WHERE s.student_name IN (SELECT t.name FROM teachers t);
+-------+
| name  |
+-------+
| Maxsu |
+-------+
1 row in set (0.10 sec)

它顯示了兩個表中name列都有存在的值 - 'Maxsu'

2. INTERSECT運算符指定WHERE條件

以下是針對INTERSECT運算符的一般查詢,但不能在MariaDB中正常使用。參考以下語句 -

-- 理想語句,但是MariaDB中不支持 
SELECT s.student_name  
FROM Students s WHERE student_id<100
INTERSECT  
SELECT t.name  
FROM teachers t
WHERE t.address = 'Haikou';

上面語句的代替語句是 -

SELECT s.student_name AS name
FROM Students s
WHERE s.student_id<100 AND s.student_name IN (SELECT t.name FROM teachers t  WHERE t.address = 'Haikou');

執(zhí)行上面示例代碼,得到以下結果 -

MariaDB [testdb]> SELECT s.student_name AS name
    -> FROM Students s
    -> WHERE s.student_id<100 AND s.student_name IN (SELECT t.name FROM teachers t  WHERE t.address = 'Haikou');
+-------+
| name  |
+-------+
| Maxsu |
+-------+
1 row in set (0.00 sec)