• Notice regarding image hosts and attachments. See here.

SQL Joins etc

Associate
Joined
7 Dec 2007
Posts
302
Location
Derbyshire, Tibshelf
Having problems with my SQL query, which seems to work fine until I try to add another table

Code:
SELECT a.u_id, a.u_name, a.team_id, b.team_id, b.team_tag, count(c.u_id), SUM(c.city_pop)
FROM user a LEFT JOIN team b ON (a.team_id = b.team_id), city c
WHERE c.u_id IS NOT NULL
AND a.u_id = c.u_id
AND a.u_id <> '1'
GROUP BY a.u_id
ORDER BY SUM(c.city_pop) DESC

But lets say I want to find another field... I add another count(d.u_id) and a.u_id = d.u_id (which I would thought just uses the ID and grabs what you need)

The results completely mess up, does something weird with the results which are totally bogus.

Code:
SELECT a.u_id, a.u_name, a.team_id, b.team_id, b.team_tag, count(c.u_id), SUM(c.city_pop), count(d.u_id)
FROM user a LEFT JOIN team b ON (a.team_id = b.team_id), city c, exp d
WHERE c.u_id IS NOT NULL
AND a.u_id = c.u_id
AND a.u_id = d.u_id
AND a.u_id <> '1'
GROUP BY a.u_id
ORDER BY SUM(c.city_pop) DESC

This also doesn't work:

Code:
SELECT a.u_id, a.u_name, a.team_id, b.team_id, b.team_tag, count(c.u_id), SUM(c.city_pop), count(d.u_id)
FROM user a LEFT JOIN team b ON (a.team_id = b.team_id) LEFT JOIN exp d ON (a.u_id = d.u_id), city c
WHERE c.u_id IS NOT NULL
AND a.u_id = c.u_id
AND a.u_id <> '1'
GROUP BY a.u_id
ORDER BY SUM(c.city_pop) DESC

Thanks, if anyone can help :/
 
Back
Top Bottom