外连接
左外/右外连接
- 左外连接:左表全部出现在结果集中,若右表无对应记录,则相应字段为
NULL
left join … on 条件
NULL
- right join … on 条件
全外连接
- 两个表均出现在结果集中,无对应记录的相应字段为
NULL
full join/full outer join … on 条件
- 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 npimport psycopg2import pandas as pdconnect = 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) # 删除缺省值,全连接会产生很多缺省# 转换成arraydf1 = np.array(df)# 转换成列表my_list = df1.tolist()print(df) # df