I have 3 tables: goods, store and pics. In the first table goods titles are stored. In the second - balance of goods in different stocks, in the third - links to goods pictures. So goods has one-to-many connection with store and pics. Now, I need to get a list of goods with sum of stock rests and count of pictures by one query. I did it like this:
SELECT good.id, good.title, sum(store.rest) AS storerest, count(pics.id) AS picscount
FROM goods
LEFT JOIN store ON (goods.id = store.goodid)
LEFT JOIN pics ON (goods.id = pics.goodid)
GROUP BY goods.id`
All seems ok while good has 0 or 1 picture. But when it has 2 - storerest doubles, and I can't understand why. What's wrong?