Records = Cursor.execute("SELECT cust_addr1, cust_addr2, cust_postcode, cust_telno_home, cust_email \
FROM tblcustomer, tblpet \
WHERE cust_surname = ? and tblpet.pet_deceased = ?" ,(SearchCriteria[0], "Y")).fetchall()
我可以运行此查询,如果我删除tblpet完全正常,但当我添加tblpet查询运行但我得到结果重复。因此,取代5个结果,我有6300这样的东西,但所有相同的结果循环。
由于
修改
我已经修复了它,感谢 Mureinik 和 Siyual 告诉我有关联接的信息。 然后我找到了这些
https://msdn.microsoft.com/en-us/library/bb243855(v=office.12).aspx
JOINing mdb tables with pyodbc
它工作了50%然后我有一个很好的想法进入访问并进行查询然后切换到SQL视图并复制它。它工作得很好,我是如此接近
ANSWER
Records = Cursor.execute("""SELECT tblcustomer.cust_addr1, tblcustomer.cust_addr2,
tblcustomer.cust_postcode, tblcustomer.cust_telno_home, tblcustomer.cust_email
FROM tblcustomer INNER JOIN tblpet ON (tblcustomer.cust_no = tblpet.pet_cust_no)
WHERE cust_surname = ? and tblpet.pet_deceased = ?""", (SearchCriteria[0],"Y")).fetchall()
答案 0 :(得分:1)
当您像在此处所做的那样编写隐式连接时(即,在from
子句中有多个表),数据库会创建行的笛卡尔积。您需要添加条件以仅匹配适当的行。例如,假设客户有ID并且宠物具有客户的ID:
SELECT cust_addr1, cust_addr2, cust_postcode, cust_telno_home, cust_email
FROM tblcustomer, tblpet
WHERE tblcustomer.id = tblpet.customer_id AND -- Here!
cust_surname = ? AND
tblpet.pet_deceased = ?
或者更好的是,您可以使用显式join
语法:
SELECT cust_addr1, cust_addr2, cust_postcode, cust_telno_home, cust_email
FROM tblcustomer
JOIN tblpet ON tblcustomer.id = tblpet.customer_id -- Here!
WHERE cust_surname = ? AND
tblpet.pet_deceased = ?