📜  Python SQLite – JOIN 子句

📅  最后修改于: 2022-05-13 01:55:11.569000             🧑  作者: Mango

Python SQLite – JOIN 子句

在本文中,我们将使用Python的sqlite3模块讨论 SQLite 中的 JOIN 子句。但首先让我们看一下关于加入 SQLite 的简介。

加入条款

JOIN 子句根据公共属性组合来自两个表的记录。不同类型的连接如下所示:

  • INNER JOIN (OR JOIN) – 给出在两个表中具有共同属性的记录。
  • LEFT JOIN –给出左表中的所有记录和右表中的公共记录。
  • RIGHT JOIN –提供右表中的所有记录,仅提供左表中的公共记录。
  • FULL OUTER JOIN –当左表或右表中存在共同属性时,给出所有记录。
  • CROSS JOIN –将一个表的记录与另一个表的所有其他记录一起提供。

笔记:

  • 与其他类型的联接不同,它不包括联接条件。
  • SQLite 不直接支持 RIGHT JOIN 和 FULL OUTER JOIN。

创建数据库

在这里,我们将创建一个具有两个表 Advisor(AdvisorID, AdvisorName) 和 Student(StudentID, StudentName, AdvisorID) 的简单数据库,其中 Student 表的 AdvisorID 是引用 Advisor 表的 AdvisorID 的外键。



Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
# New file created if it doesn't already exist
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Create and populate tables
cursor.executescript('''
CREATE TABLE Advisor(
AdvisorID INTEGER NOT NULL,
AdvisorName TEXT NOT NULL,
PRIMARY KEY(AdvisorID)
);
  
CREATE TABLE Student(
StudentID NUMERIC NOT NULL,
StudentName NUMERIC NOT NULL,
AdvisorID INTEGER,
FOREIGN KEY(AdvisorID) REFERENCES Advisor(AdvisorID),
PRIMARY KEY(StudentID)
);
  
INSERT INTO Advisor(AdvisorID, AdvisorName) VALUES
(1,"John Paul"), 
(2,"Anthony Roy"), 
(3,"Raj Shetty"),
(4,"Sam Reeds"),
(5,"Arthur Clintwood");
  
INSERT INTO Student(StudentID, StudentName, AdvisorID) VALUES
(501,"Geek1",1),
(502,"Geek2",1),
(503,"Geek3",3),
(504,"Geek4",2),
(505,"Geek5",4),
(506,"Geek6",2),
(507,"Geek7",2),
(508,"Geek8",3),
(509,"Geek9",NULL),
(510,"Geek10",1);
  
''')
  
#Commit changes to database
conn.commit()
  
# Closing the connection
conn.close()


Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for INNER JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
INNER JOIN Advisor
ON Student.AdvisorID = Advisor.AdvisorID;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()


Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for LEFT JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
LEFT JOIN Advisor
USING(AdvisorID) ;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()


Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for RIGHT JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Advisor 
LEFT JOIN Student
USING(AdvisorID);'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()


Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for FULL OUTER JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
LEFT JOIN Advisor
USING(AdvisorID)
UNION ALL
SELECT StudentID, StudentName, AdvisorName 
FROM Advisor 
LEFT JOIN Student
USING(AdvisorID)
WHERE Student.AdvisorID IS NULL;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()


Python3
# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for CROSS JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
CROSS JOIN Advisor;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()


创建的表:

顾问表

学生桌

现在,让我们对上面创建的数据库执行不同类型的连接。

内部联接

内连接也表示为连接,它给出了在两个表中具有共同属性的记录。

蟒蛇3

# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for INNER JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
INNER JOIN Advisor
ON Student.AdvisorID = Advisor.AdvisorID;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()

输出:

左加入

给出左表中的所有记录,仅给出右表中的公共记录。

蟒蛇3

# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for LEFT JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
LEFT JOIN Advisor
USING(AdvisorID) ;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()

由于连接表的列名 (AdvisorID) 相同,因此可以使用子句 USING(AdvisorID) 代替 ON Student.AdvisorID = Advisor.AdvisorID。

输出:

右加入

给出右表中的所有记录,仅给出左表中的公共记录。如前所述,SQLite 不直接支持 RIGHT JOIN。但是,可以使用 LEFT JOIN 通过切换 student 和 advisor 表的位置来模拟它。

蟒蛇3

# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for RIGHT JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Advisor 
LEFT JOIN Student
USING(AdvisorID);'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()

输出:

全外连接

当左表或右表中存在共同属性时,给出所有记录。如前所述,SQLite 不直接支持 FULL OUTER JOIN。但是,它可以使用 LEFT JOIN 进行模拟。在这个查询中,第二个 SELECT 语句交换了 student 和 advisor 表的位置。 UNION ALL 子句保留两个 SELECT 查询结果中的重复行。第二个 SELECT 语句中的 WHERE 子句删除已经包含在第一个 SELECT 语句的结果集中的行。

蟒蛇3

# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for FULL OUTER JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
LEFT JOIN Advisor
USING(AdvisorID)
UNION ALL
SELECT StudentID, StudentName, AdvisorName 
FROM Advisor 
LEFT JOIN Student
USING(AdvisorID)
WHERE Student.AdvisorID IS NULL;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()

输出:

交叉连接

它将一个表的所有记录与另一个表的所有其他记录组合在一起,也就是说,它从连接表中创建记录的笛卡尔积。

蟒蛇3

# Import required libraries
import sqlite3
  
# Connect to SQLite database
conn = sqlite3.connect(r'C:\Users\SQLite\Geeks.db')
  
# Create cursor object
cursor = conn.cursor()
  
# Query for CROSS JOIN
sql = '''SELECT StudentID, StudentName, AdvisorName 
FROM Student 
CROSS JOIN Advisor;'''
  
# Executing the query
cursor.execute(sql)
  
# Fetching rows from the result table
result = cursor.fetchall()
for row in result:
    print(row)
  
# Closing the connection
conn.close()

输出: