Rexolio
Technical User
- Aug 29, 2001
- 230
I have 3 tables that go like this (more or less):
The Primary key in Tables 1 & 2 are ID. These are the same and are created when a new record is entered into Table 1. The PK's in Table 3 are ID and CategoryID. ID holds all of the records together in a relationship.
The problem I'm having is I need be able to do a query on all 3 tables looking for a number of things, but in the end I only want 1 record. The problem I'm having is that when I do a search for categories where, for example, CategoryID = 1, 3 or 6, I get back duplicates. In this case, with the records above, I would get back 2 records for ID 1, 1 record for ID 2, and 2 records for ID 3. I don't want duplicates. Here is the query:
This query gives me duplicates. I've even tried saying:
and of course that doesn't work either.
Thanks in advance.
![[bugeyed] [bugeyed] [bugeyed]](/data/assets/smilies/bugeyed.gif)
rexolio@yahoo.com
"I'm not dumb. I just have a command of thoroughly useless information." - Calvin, of Calvin and Hobbes
Code:
TABLE 1
ID NAME CITY ST
1 J. Doe Boise ID
2 W. Smith Tampa FL
3 B. Jones Atlanta GA
TABLE 2
ID SPOUSE INCOME DATE
1 Mary 55000 1/1/2004
2 Bob 74000 1/14/2004
3 Tina 60000 1/15/2004
TABLE 3
ID CATEGORYID
1 1
1 3
2 2
2 3
2 4
3 1
3 2
3 5
3 6
The Primary key in Tables 1 & 2 are ID. These are the same and are created when a new record is entered into Table 1. The PK's in Table 3 are ID and CategoryID. ID holds all of the records together in a relationship.
The problem I'm having is I need be able to do a query on all 3 tables looking for a number of things, but in the end I only want 1 record. The problem I'm having is that when I do a search for categories where, for example, CategoryID = 1, 3 or 6, I get back duplicates. In this case, with the records above, I would get back 2 records for ID 1, 1 record for ID 2, and 2 records for ID 3. I don't want duplicates. Here is the query:
Code:
SELECT A.*, B.*, C.* FROM TABLE1 A
INNER JOIN TABLE2 B ON A.ID = B.ID
INNER JOIN TABLE3 C ON A.ID = C.ID
WHERE (A.ST <> 'TN') AND (B.DATE >= '1/1/2004') AND
(C.CATEGORYID = 1 OR C.CATEGORYID = 3 OR C.CATEGORYID = 6)
ORDER BY A.DATE
This query gives me duplicates. I've even tried saying:
Code:
...WHERE (A.ST <> 'TN') AND (B.DATE >= '1/1/2004') AND
(C.CATEGORYID = 1)
OR (A.ST <> 'TN') AND (B.DATE >= '1/1/2004') AND
(C.CATEGORYID = 3)
OR (A.ST <> 'TN') AND (B.DATE >= '1/1/2004') AND
(C.CATEGORYID = 6)
ORDER BY A.DATE
and of course that doesn't work either.
![[bugeyed] [bugeyed] [bugeyed]](/data/assets/smilies/bugeyed.gif)
rexolio@yahoo.com
"I'm not dumb. I just have a command of thoroughly useless information." - Calvin, of Calvin and Hobbes