Python通过EXCEL生成SQL语句-多表关联查询
#携程暑期实习##打开Python的大门##Python#import pandas as pd class MultiTableAssociationQuery(object): def __init__(self): self.notes = "-- 多表关联查询" self.start = f"WITH cursor AS(" self.end = f")SELECT count(*) FROM cursor" self.df = pd.read_csv(r'D:\sql.csv', encoding='gb18030') self.text = " " def multi_table_query(self): df = self.df print(self.notes) print(self.start) for index, rows in df.iterrows(): if index != df.index[-1]: if str(rows[2]) == 'nan': text = f"{self.text}SELECT '{rows[0]}.{rows[1]}' AS table_name,count(*) AS count \n FROM {rows[0]}.{rows[1]} \n UNION ON" print(text) else: text = f"{self.text}SELECT '{rows[0]}.{rows[1]}' AS table_name,count(*) AS count \n FROM {rows[0]}.{rows[1]} \n WHERE dt ='{rows[2]}' \n UNION ON" print(text) else: if str(rows[2]) == 'nan': text = f"{self.text}SELECT '{rows[0]}.{rows[1]}' AS table_name,count(*) AS count \n FROM {rows[0]}.{rows[1]}" print(text) else: text = f"{self.text}SELECT '{rows[0]}.{rows[1]}' AS table_name,count(*) AS count \n FROM {rows[0]}.{rows[1]} \n WHERE dt ='{rows[2]}'" print(text) print(self.end) if __name__ == '__main__': t = MultiTableAssociationQuery() t.multi_table_query()
Python 文章被收录于专栏
Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发


