Reputation: 455
I have two tables, for simplicity let's call them table A and table B. Table A contains 2 rows of values, and table B contains 4 rows of values. Table A is linked to Table B through a foreign key constraint, linked to Table B's primary key. However, if I try to run a query selecting every row from both of these columns, for each row in Table A it runs through every row in table B, for example:
Is there a problem with my tables here? Or is it the case of a bad query?
Upvotes: 0
Views: 62
Reputation: 626
select a.colA, a.colB, b.colA, b.colB, b.colC, b.coldD from TableA a, TableB b where a.colA=b.colA;
Assuming colA should be primary/foreign key in each of the tables.
Upvotes: 1
Reputation: 41
Try this :
(Change ColumnValue, NameOfForeignKey and PrimaryKey by the correct column name)
Solution 1 :
SELECT TableA.ColumnValue, TableB.ColumnValue
FROM TableA
INNER JOIN TableB ON TableA.NameOfForeignKey = TableB.PrimaryKey
Solution 2 :
SELECT TableA.ColumnValue, TableB.ColumnValue
FROM TableA, TableB
WHERE TableA.NameOfForeignKey = TableB.PrimaryKey
Upvotes: 0