i need to join 4 tables. I have users table and need to connect with customers and than both connect to profiles for that i using relations table and later get the sum.
My result would be as good
2 Thomas White 2.56 180.00 461.89 0.00 0.00 0.00 antras komentaras
3 Doina Slaivici 5.87 161.17 946.88 0.00 0.00 vienas komentaras
but problem is with profiles comments im getting only from single profile comment, not from all, because some users have few profiles.
My join:
SELECT
*,
users.id AS uid,
users.sum_per_hour AS usum_per_hour,
SUM(
customers.sum / 4.33 / customers.days_per_week * profiles.days
) AS total_sum
FROM
users,
relations,
customers,
profiles
WHERE users.id = relations.uid
AND customers.id = relations.cid
AND profiles.uid = relations.uid
AND profiles.cid = relations.cid
GROUP BY users.id
try changing your FROM to this
SELECT
*,
u.id AS uid,
u.sum_per_hour AS usum_per_hour,
SUM(c.sum / 4.33 / c.days_per_week * p.days) AS total_sum
FROM users u
LEFT JOIN relations r
ON u.id = r.uid
LEFT JOIN customers c
ON c.id = r.cid
LEFT JOIN profiles p
ON p.uid = r.uid
AND p.cid = r.cid -- # -- this may need to be OR instead of AND
GROUP BY u.id
I honestly don't know if this will solve your issue... you have to ask a better question.. meaning show your result you are currently getting.. post the data (not images) or even better post a sqlfiddle so we can use it ourselves.. and then show your expected result. but this is a guess from what I can see