python数据分析一周速成2.连表查询【含数据库实战项目】

连表查询

结合数据库实战(sql和hive跨库取数)

数据准备

# 前面省略数据库连接,提示:可以用pymysql和pyhive模块
pre_sql = """
select ap,time from biaoyi a
where a.time >20250101
"""
sql_df = run_mysql(pre_sql)

pre_hive="""
select 
application_number 
,activation_dte 
from  biaoer b 
where b.application_number in (xx)
"""

好理解的方案一

join连接字符串后,replace

ap_num = ','.join(f"'{
     num}'" for num in sql_df.iloc[:, 2] )  

pre_hive=pre_hive.replace('xx',ap_num)

hive_df = run_presto(pre_hive)

简洁的方案二

tolist直接转成字符串的列表[‘xxx’,‘xxx,’‘xxx’…]

pre_hive=pre_hive.replace('xx',str(sql_df['ap'].unique().tolist()

你可能感兴趣的:(数据分析,数据分析,CDA,python)