views:

128

answers:

5

I am using a query like this on my postgres database,

SELECT TableA.id FROM TableA , TableB WHERE TableA.id = 100;

Each TableA.id is unique (its an autoincrement), I am gettting more than 1 result. Am i missing something in here?

+6  A: 

You're doing a cross-join - effectively every row in TableB against the single row in TableA. If you select something from TableB as well, this will be more obvious :) The fact that you don't currently select anything from TableB doesn't stop the (TableA, TableB) pairs from being the result of the join, before the projection.

Jon Skeet
+6  A: 

You need a join:

SELECT TableA.ID from TableA
INNER JOIN TableB 
ON TableB.TableAID = TableA.ID 
WHERE TableA.ID = 100
Ben McEvoy
+1  A: 

which is the relation between TableA and TableB?

you might need to do something like this

Where TableA.id = 100 And TableB.TableA_Id = TableA.id

Pablo Fernandez
+3  A: 

You're getting one row from TableA but all rows from TableB. Perhaps you meant:

SELECT TableA.id FROM TableA, TableB WHERE TableA.id=TableB.id AND TableA.id = 100
Jason Cohen
A: 

You need a join before the where clause:

INNER JOIN TableB ON TableA.Id = TableB.Id
unochild