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

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

表结构:
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()
【热门文章】
【热门文章】