2016-10-27 4 views
0
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과 같은 것을 가졌지 만 모든 동일한 결과가 반복되었습니다.Pyodbc SQL SELECT Cursor.execute 중복 결과

감사

편집

지금 조인에 대해 말해위한 MureinikSiyual에에게 감사를 해결했습니다. 나는 이들

https://msdn.microsoft.com/en-us/library/bb243855(v=office.12).aspx

JOINing mdb tables with pyodbc

그것은 50 % 일 BU 그럼 내가 액세스에 가서 만들기 쿼리를 다음 SQL보기로 전환하고 복사 좋은 생각을 가지고 있었다 발견했다. 그것은 잘 작동하고 난 당신이 여기처럼 암시 가입 쓸 때

답변

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

: 당신은 명시 적 join 구문을 사용할 수 있습니다, 더 나은 아직

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'. 테이블을'JOIN'하려는 경우 명시 적'JOIN' 구문을 사용하십시오. (FROM 절에 쉼표를 사용하지 마십시오 - [심각하게는 1992 년에 사용되지 않았습니다] (http : //www.contrib.andrew. cmu.edu/~shadow/sql/sql1992.txt)) 'ON' 절에서 테이블이 어떻게 관련되어 있는지 보여줍니다. – Siyual

+0

감사합니다. 질문이 업데이트되었습니다. –

답변

1

(즉이는 from 절에 하나 개 이상의 테이블이), 데이터베이스가 생성 너무 너무 가까이 행의 데카르트 곱. 해당 행만 일치시키는 조건을 추가해야합니다. 예를 들면, 고객을 가정하는 것은 ID를 가지고 있으며, 애완 동물은 고객의 ID가 : 이것은`CROSS을하고 있기 때문에

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 = ? 
+0

질문이 업데이트되었습니다. –