外连接
左外/右外连接
- 左外连接:左表全部出现在结果集中,若右表无对应记录,则相应字段为
NULL
- 右外连接:右表全部出现在结果集中,若左表无对应记录,则相应字段为
NULL
全外连接
- 两个表均出现在结果集中,无对应记录的相应字段为
NULL
- full join/full outer join ... on 条件
- pg中基于行数进行全外连接:
- create table join_1 as (select * from(select *, row_number()over() rn from customer) as a full join(select *, row_number()over() rn1 from district) as b on a.rn=b.rn1);
python连接pg数据库
点击查看代码
# 外连接
## 左外/右外连接
* 左外连接:左表全部出现在结果集中,若右表无对应记录,则相应字段为`NULL`
* left join ... on 条件
* 右外连接:右表全部出现在结果集中,若左表无对应记录,则相应字段为`NULL`
* right join ... on 条件
## 全外连接
* 两个表均出现在结果集中,无对应记录的相应字段为`NULL`
* full join/full outer join ... on 条件
* pg中基于行数进行全外连接:
* create table join_1 as (select * from(select *, row_number()over() rn from customer) as a full join(select *, row_number()over() rn1 from district) as b on a.rn=b.rn1);
# python连接pg数据库
import numpy as np
import psycopg2
import pandas as pd
connect = psycopg2.connect(database='test',
user='Sevent',
password='',
port='5432'
)
# 创建一个cursor来执行数据库的操作
cur = connect.cursor()
sql = "SELECT 目标列 FROM full_join"df = pd.read_sql(sql, con=connect)
# df.dropna(axis=0, how='any', inplace=True) # 删除缺省值,全连接会产生很多缺省
# 转换成array
df1 = np.array(df)
# 转换成列表
my_list = df1.tolist()
print(df) # df