[英]How to print the results of a left join
I'm having issues showing the results of a left join in python.我在显示 python 中左连接的结果时遇到问题。 Here's the issue:
这是问题:
I have two tables.我有两张桌子。 One Customers table and one Weights table that keep track of weights records for each customer.
一个客户表和一个权重表,用于跟踪每个客户的权重记录。
Customers table:客户表:
pID Name
01 John
02 Charlotte
Weights table:重量表:
wID pID Weight
01 01 90
02 01 93
03 01 92
04 02 76
05 02 74
When i store my left join in a python cursor i get this result.当我将左连接存储在 python 游标中时,我得到了这个结果。
pID Name wID Weight
01 John 01 90
01 John 02 93
01 John 03 92
02 John 04 76
02 John 05 74
My goal is to show:我的目标是展示:
pID: 01
Name: John
wID: 01 Weight: 90
wID: 02 Weight: 93
wID: 03 Weight: 92
pID: 02
Name: Charlotte
wID: 04 Weight: 76
wID: 05 Weight: 74
Now the Python code.现在是 Python 代码。 I have a Customer class that stores the person ID, Name and an array holding all of the weights registered for that person.
我有一个 Customer 类,用于存储人员 ID、姓名和一个数组,其中包含为该人员注册的所有权重。
So this is my attemp to store all of the customers and their weights in a single list called customers.所以这是我尝试将所有客户及其权重存储在一个名为客户的列表中。
customers = []
query = "SELECT * FROM customers LEFT JOIN weights ON customers.idcustomers = weigths.idcustomers"
try:
self.cur = self.connect()
self.cur.execute(query)
queryResults = self.cur.fetchall()
self.desconectar()
except Exception as e:
print(e)
pass
for row in queryResults:
found = False
for cus in customers:
if row[0] == cus.id:
cus.weights.append(Weight(row[0],row[2],row[3]))
found = True
if found == False:
newCustomer = Customer(row[0],row[1])
newCustomer.weights.append(Weight(row[0],row[2],row[3]))
customers.append(newCustomer)
return customers
For some reason i get all of the weights added into each of the customers.出于某种原因,我将所有权重添加到每个客户中。
Results look like this:结果如下所示:
pID: 01
Name: John
wID: 01 Weight: 90
wID: 02 Weight: 93
wID: 03 Weight: 92
wID: 04 Weight: 76
wID: 05 Weight: 74
pID: 02
Name: Charlotte
wID: 01 Weight: 90
wID: 02 Weight: 93
wID: 03 Weight: 92
wID: 04 Weight: 76
wID: 05 Weight: 74
I've been trying for a while to fix this and i don't really know why each person is storing every weight.我已经尝试了一段时间来解决这个问题,但我真的不知道为什么每个人都在储存每一个重量。
This code is a simplified version of the original script, i hope i didn't mess up!此代码是原始脚本的简化版本,希望我没有搞砸!
Any help is welcome.. thanks in advance!欢迎任何帮助......提前致谢!
EDIT: The query results are right, i made a mistake translating the SQL line.编辑:查询结果是正确的,我在翻译 SQL 行时犯了一个错误。
what's failing is the python code.失败的是python代码。
Make sure you join pID
on both tables.确保在两个表上都加入
pID
。
I'm not sure how you've defined the classes, but seems like the JOIN
condition on your query
it's joining pID
with wID
:我不知道你是如何定义的类,但好像
JOIN
您的病情query
它的加入pID
与wID
:
query = "SELECT * FROM customers LEFT JOIN weights ON customers.idcustomers = weighs.idweights"
So, check the result of your LEFT JOIN
as it is to make sure the result it's as you expect.因此,请检查
LEFT JOIN
的结果,以确保结果符合您的预期。
Then, move onto the Python
logic of appending the results.然后,转到附加结果的
Python
逻辑。 But I think that should be fine.不过我觉得应该没问题。
PS: Is there any difference between the classes Weight
and Peso
? PS:类
Weight
和Peso
之间有什么区别吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.