I am very new to SQL
when I execute the below query it returns the following results:
SELECT table1.image_name, table1.item_id
FROM table1, table2, table3
WHERE table2.catagory="item"
AND table3.account_id=59
image_name image_id
-------------------------
d9.jpg 89
d9.jpg 89
d9.jpg 89
d9.jpg 89
d10.jpg 90
d10.jpg 90
d10.jpg 90
d10.jpg 90
etc.....
The results repeat the same attribute four times! I don’t understand why this is and I cannot find a reason for this anywhere!
I have 3 tables, table1 PK(image_id) is referenced as the FK in Item. Item PK(Item_id) is referenced with FK in table3. table3 has a compound key from table 4(account_id) and table2(item_id).
My query is clearly wrong, but I don’t understand why? I hope someone could provide an explanation to the result!, or help point me in the right direction!
Thanks
You get multiple results because you do not specify join conditions. Foreign key relations are good, but you need to specify that they apply in your query:
Note the last two conditions. You need to change them to match your needs.