Python PostgreSQL – 加入
在本文中,我们将看到在 PostgreSQL 中使用Python的pyscopg2 的连接方法。让我们看看 PostgreSQL 支持的连接类型。
加盟类型:
- 内部联接
- 全联接(外联接)
- 左连接
- 右连接
- 交叉连接
示范表:
表 1:员工表
表 2:部门表
psycopg2.connect() 方法用于连接到数据库,cursor() 和 fetchall() 方法用于从数据库中检索数据。我们使用 execute() 方法来执行我们的 SQL 命令,然后通过 fetchall() 方法进一步检索它。
内部联接
内连接是最常见的连接类型之一。内连接用于根据行之间的共同特征连接两个表。它返回一个具有共同行特征的表。
执行一条 SQL 语句:
SELECT table1.col1, table2.col2…
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;
代码:
Python3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee INNER JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
Python3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee FULL JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
Python3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee left JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
Python3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee RIGHT JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
Python3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT employee.empno,employee.ename,
dept.deptno from employee cross JOIN dept '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
输出:
完全加入
它也称为“完全外部联接”,它返回所有在左表或右表中匹配的数据。如果两个表中的行不匹配,则生成的数据框将用表中缺少匹配行的每一列替换 NaN。
执行一条 SQL 语句:
SELECT table1.col1, table2.col2…
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
代码:
蟒蛇3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee FULL JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
输出:
左加入
它也称为左外连接,返回一个包含左数据框所有行的表。如果左表存在不匹配的行,则右表中不匹配的数据将替换为 NaN。
执行一条 SQL 语句:
SELECT table1.col1, table2.col2…
FROM table1
LEFT JOIN table2
ON table1.common_field = table2.common_field;
代码:
蟒蛇3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee left JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
输出:
右加入
右连接与左连接完全相反。返回一个包含右表所有行的表。如果右表存在不匹配的行,则左表中不匹配的数据将替换为 NaN。
执行一条 SQL 语句:
SELECT table1.col1, table2.col2…
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
代码:
蟒蛇3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT * from employee RIGHT JOIN dept\
ON employee.deptno =dept.deptno '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
输出:
交叉连接
交叉联接将第一个表的每一行与第二个表的每一行进行匹配。如果输入表分别有 A 和 B 列,那么我们最终的输出表将有 A+B 列
执行一条 SQL 语句:
SELECT COLUMNS… FROM table1 CROSS JOIN table2
代码:
蟒蛇3
import psycopg2
conn = psycopg2.connect(
database="EMPLOYEE_DATABASE", user='postgres',
password='pass', host='127.0.0.1', port='5432'
)
conn.autocommit = True
cursor = conn.cursor()
sql = '''SELECT employee.empno,employee.ename,
dept.deptno from employee cross JOIN dept '''
cursor.execute(sql)
results = cursor.fetchall()
for i in results:
print(i)
conn.commit()
conn.close()
输出: