Python MySQLdb模块多表连接查询结果

 u44093631 发布于 2022-11-06 01:53

表结构:
post:

+---------+----------+------+-----+---------+----------------+
| Field   | Type     | Null | Key | Default | Extra          |
+---------+----------+------+-----+---------+----------------+
| id      | int(11)  | NO   | PRI | NULL    | auto_increment |
| name    | char(30) | YES  |     | NULL    |                |
| user_id | int(11)  | YES  |     | NULL    |                |
+---------+----------+------+-----+---------+----------------+

user:

+---------+----------+------+-----+---------+----------------+
| Field   | Type     | Null | Key | Default | Extra          |
+---------+----------+------+-----+---------+----------------+
| user_id | int(11)  | NO   | PRI | NULL    | auto_increment |
| name    | char(30) | YES  |     | NULL    |                |
| email   | char(30) | YES  |     | NULL    |                |
+---------+----------+------+-----+---------+----------------+

查询结果是数据dict的元组。

但是我觉得这个数据dict的keys很难以琢磨:

['post.user_id', 'user_id', 'name', 'email', 'post.name', 'id']

问题就是:我怎么知道获取相应数据的key是什么呢? 有的key是table.name形式的,有的不是!

我测试的代码:

import MySQLdb

import MySQLdb.cursors

con = MySQLdb.connect(user = "root", passwd = "123456", db = "mydb", cursorclass=MySQLdb.cursors.DictCursor)

cur = con.cursor()

cur.execute("select * from user, post where user.user_id = post.user_id")

print cur.fetchone().keys()
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有