我有 2 张桌子。在一张表中,我有关于产品的详细信息,例如:book 它包含国际标准书号。它是外键。在另一张桌子上,我有订购书的详细信息。例如:订购图书的用户的用户 ID 和 isbn 编号。我想向用户展示订购的产品。所以我需要从订单表中获取 isbn 编号,然后从该 isbn 编号中获取 book 表中的数据。这是给python烧瓶的。当两个表中的用户 ID 都不常见时,我不知道如何从另一个表中获取数据@app.route('/myorder',methods=['GET','POST'])def myorder():    if request.method == 'GET':        cur=connection.cursor()        user_id=session['userid']        cur.execute("SELECT ISBN FROM orders WHERE user_id=%s",[user_id])        data=cur.fetchall()        cur.close()    return render_template('myorder.html',data=data)我期望一个查询来获取数据
                    
                    
                3 回答
 
                    
                    
                            斯蒂芬大帝
                            
                                
                            
                        
                        
                                                
                    TA贡献1827条经验 获得超8个赞
您应该从我猜的连接查询中选择书籍详细信息:
select books.* from books join orders on books.isbn = orders.isbn
where orders.user_id = ?;
 
                    
                    
                            慕尼黑5688855
                            
                                
                            
                        
                        
                                                
                    TA贡献1848条经验 获得超2个赞
因此,您拥有的是一个带有书籍详细信息和 ISBN 号的表格以及另一个带有用户和 ISBN 号的表格。因此,获取本书详细信息的最简单方法是:
cur.execute("select booktable.bookdetails from booktable A, UserTable B
where A.ISBNno = B.ISBNno
and B.userID = %s", [user_id])
或者如果你想要所有的列
cur.execute("select booktable.* from booktable A, UserTable B
where A.ISBNno = B.ISBNno
and B.userID = %s", [user_id])
 
                    
                    
                            慕标琳琳
                            
                                
                            
                        
                        
                                                
                    TA贡献1830条经验 获得超9个赞
这将做同样的select books.* from books where isbn in (select isbn from orders where user_id = ?)......
添加回答
举报
0/150
	提交
		取消
	