使用 Python MySQLdb 执行多 table 查询

Executing multi-table queries with Python MySQLdb

我一直在尝试 return 来自两个不同 table 的值,但似乎无法将 c.execute(查询)函数获取到 return我想要它做什么。目前我的代码将 return 第一个 c.fetchone()[0],但第二个 fetchone()[5] 给出了一个错误,它超出了范围,这意味着它可能仍在尝试获取数据来自我的 'clients' table,它没有 6 列。我不认为我完全理解 MySQLdb 是如何工作的,它很神奇,但是找不到任何 multi-table 查询的好例子。下面是我的代码片段!谢谢!

c, conn = connection()

            #check if already exists
            x = c.execute("SELECT * FROM clients WHERE email = (%s)", (thwart(email),))

            if int(x) > 0:
                flash("That email already has an account, please try a new email or sign in.")
                return render_template('register.html', form=form)
            else:
                c.execute("INSERT INTO clients (email, phone, password) VALUES (%s, %s, %s)", (thwart(email), thwart(phone), thwart(password)))
                c.execute("SELECT cid FROM clients WHERE email = (%s)", (thwart(email),))
                clientcid = c.fetchone()[0]
                c.execute("INSERT INTO cpersonals (first_name, last_name, address, zip) VALUES (%s, %s, %s, %s)", (thwart(first_name), thwart(last_name), thwart(address), czip))
                c.execute("SELECT reg_date FROM cpersonals WHERE cid = (%s)", (clientcid,))
                reg_date = c.fetchone()[5]
                rating = c.execute("SELECT rating FROM clients WHERE email = (%s)", (thwart(email),))
                conn.commit()
                flash("Thanks for registering!")
                c.close()
                conn.close()

您的查询是SELECT reg_date FROM cpersonals ...。您只选择了一列。 fetchone()[5] 失败的原因是,获取的记录中没有第 6 列。尝试 0 代替 5

您为什么使用 5