Querying data from different tables
I am using a query like this in my postgres database,
SELECT TableA.id FROM TableA, TableB WHERE TableA.id = 100;
Each TableA.id is unique (its autoincrement), I get more than one result. Am I missing something here?
You need a connection:
SELECT TableA.ID from TableA
INNER JOIN TableB
ON TableB.TableAID = TableA.ID
WHERE TableA.ID = 100
You are doing a cross join - effectively each row in TableB versus one row in TableA. If you also select something from TableB, it will be more obvious :) The fact that you are not currently selecting anything from TableB does not stop pairs (TableA, TableB) as a result of the union from being projected.
You get one line from TableA
, but all lines from TableB
. Did you mean:
SELECT TableA.id FROM TableA, TableB WHERE TableA.id=TableB.id AND TableA.id = 100
which is the relationship between TableA and TableB?
you may need to do something like this
Where TableA.id = 100 AND TableB.TableA_Id = TableA.id
You need a join before the where clause:
INNER JOIN TableB ON TableA.Id = TableB.Id