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()
I can run this query if i remove the tblpet perfectly fine but when i add the tblpet the query runs but i get the results dupliated. So instead of 5 results i had something like 6300 but all of the same results looped.
Thanks
EDIT
I have now fixed it thanks to Mureinik and Siyual for telling me about joins. I then found these
https://msdn.microsoft.com/en-us/library/bb243855(v=office.12).aspx
JOINing mdb tables with pyodbc
It worked 50% bu then i had a great idea of going into access and making the query then switching to SQL view and copying it. It works great and i was so so close
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()
When you write an implicit join like you did here (ie, have more than one table in the from
clause), the database creates a Cartesian product of the rows. You need to add a condition to match only the appropriate rows. Eg, assuming the customer has an ID and the pet has the ID of the customer:
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 = ?
Or better yet, you can use the explicit join
syntax:
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 = ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.