python - Python:如何在 mysql 结果头中包含表前缀
问题描述
我需要在 python 中执行以下 SQL 查询。
SELECT table_a.company, table_b.company
FROM table_a
LEFT JOIN table_b ON table_a.id = table_b.id
但问题是,当我运行我的代码(尝试使用pymysql
(cursor.execute)和pandas
(pd.read_sql))时,他们显示
| company | company |
-----------------------------------
| <some company> | <some company> |
然而,我的期望是,输出应该是这样的:
| table_a.company | table_b.company |
---------------------------------------------------
| <some company> | <some company> |
(表头包含表前缀)
我如何实现这一目标?
我的条件:
- 我避免使用别名,因为真正的查询包含通配符
table_a.*
- 我想试试
mysql.connector
,但我听说你需要使用 64 位的 python。
就我而言,我需要留在 python 32 位,所以我不能使用它。
以下是我的代码:
- 熊猫
engine = sqlalchemy.create_engine(connection_string)
df = pd.read_sql(text(query), engine, chunksize=chunksize):
print(df)
- pymysql
engine = pymysql.connect(host=host, user=user, password=passw, port=port, db=database)
cursor = engine.cursor()
cursor.execute(query)
result = cursor.fetchmany(chunksize)
num_fields = len(cursor.description)
field_names = [i[0] for i in cursor.description]
df = pd.DataFrame(columns=field_names, data=result)
print(df)
任何帮助,将不胜感激
解决方案
推荐阅读
- flutter - 我如何在 Column Flutter 中使用 Gridview.count
- c# - 如何将代码 YouTube API 从控制台转换为 WPF
- html - 处理加载 API 和 CORS 时遇到问题
- java - @Retryable 和 @Scheduled 不会触发
- java - 树不会在世界上生成(我的世界织物改装)
- java - 通过命令行运行 Java/Selenium/Cucumber 测试
- unit-testing - 如何存根调用 GitHub 进行测试?
- angular - 无法解析 ApplicationModule 的所有参数:(?)。在带有 Jest 单元测试的 Angular 项目中
- html - 如何在 Angular 的起始站点上设置背景图像
- python - 如何根据python输出更改html超链接?