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?

0


source to share


5 answers


You need a connection:



SELECT TableA.ID from TableA
INNER JOIN TableB 
ON TableB.TableAID = TableA.ID 
WHERE TableA.ID = 100

      

+7


source


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.



+6


source


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

      

+3


source


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

+1


source


You need a join before the where clause:

INNER JOIN TableB ON TableA.Id = TableB.Id

      

0


source







All Articles