tags:

views:

74

answers:

3

Err I apologize for the title, but I forgot what its actually called in SQL lingo.

Thing is using this query:

SELECT DISTINCT a.col1, a.col2,a.col3,c.col1, c.col2

FROM table1 a

LEFT JOIN table2 c ON a.col1 = c.col3

WHERE a.col2 = '038CQH'

I get a result with around 56000 rows, but when I checked using:

SELECT a.col1, a.col2,a.col3 FROM table1 a where a.col2='038CQH'

I get only 4 rows.

+1  A: 

its the left join (left join is actually left outer join). It means - get all records from table1 and all the records from table2, but where a.col1 != c.col3, put nulls on all table2 columns.

Maybe an inner join is what you need? it would only add the columns from table2 where a.col1 = c.col3

Gabriel McAdams
-1 Left join takes all the records from the first (left) table
Christopherous 5000
@Christopherous 5000: I was less clear then I could be - I edited my answer to be more clear.
Gabriel McAdams
i also used inner join and a similar query without joins, using only "where a.col2 = '038CQH' and a.col1 = c.col3"
lock
+2  A: 

It is because your 4 records from table1 join to many records in table2 based on the records in table2 where col3 matches col1 from table1. BTW - these are horrible column names.

Christopherous 5000
err those are just for example purposes
lock
Christopherous 5000 is right, you are getting the Cartesian product of the two tables.Since you say there are 4 rows in table1, there must be 56000/4 rows in table2
Michael Sharek
maybe i am not just that versed on db2, because the same query in postgres returns only the 4 rows that i need. is there anything else i need to do to make it work for db2?
lock
@lock look closely at the relationships and ensure you are joining on the right columns. Also - I'm pretty sure postgres and db2 has the ANSI standard implementation of join syntax. So if you are truly using the exact same query for both - either the schema or the data between the two is different
Christopherous 5000
A: 

A slightly different approach - re-run your query:

SELECT a.col1, a.col2,a.col3 FROM table1 a where a.col2='038CQH'

Note down the four values of a.col1 returned as [a1], [a2], [a3] and [a4].

Then try running the following query against table2:

SELECT DISTINCT c.col1, c.col2
table2 c WHERE c.col3 IN ([a1], [a2], [a3], [a4])

(where [a1] etc have been replaced with the previously noted values.)

Mark Bannister