Forum Moderators: coopster

Message Too Old, No Replies

mysql statement help

         

FiRe

11:23 am on Aug 9, 2006 (gmt 0)

10+ Year Member



I have 3 tables:

+---------------------+
¦ groups ¦
+---------------------+
¦ id ¦ name ¦
+---------------------+

+---------------------+
¦ groupies ¦
+---------------------+
¦ userid ¦ groupid ¦
+---------------------+

+---------------------+
¦ users ¦
+---------------------+
¦ id ¦ user ¦ money ¦
+---------------------+

I am trying to list the top 10 groups by the total amount of money all of the users have in each group.
Its quite complicated and confusing the SQL statement with all the joins and stuff, so could someone help me put it right?

I have this so far:

SELECT groupies.groupid FROM groupies INNER JOIN users ON groupies.userid = users.id

But its not much use.
Thanks!

FalseDawn

9:32 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



SELECT GR.name, SUM(U.money) FROM ((groups GR INNER JOIN groupies GP ON GR.id=GP.groupid) INNER JOIN users U ON GP.userid=U.id) GROUP BY GR.name ORDER BY SUM(U.money) DESC LIMIT 10

FiRe

8:16 am on Aug 10, 2006 (gmt 0)

10+ Year Member



thanks but doesnt seem to work, phpmyadmin gives:

#1111 - Invalid use of group function

the_nerd

10:03 am on Aug 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the 1111 error seams to stem from using the aggregate function (sum) in the order statement. Try this instead:

select sum(money) as m, groups.groupid
from groups join
groupies on groups.groupid = groupies.groupid join
users on groupies.userid = users.userid
group by groups.groupid
order by m desc
limit 0,10

pls note: I changed your id names to groupid and userid - makes the queries a lot easier to read.

have fun,

nerd.

FiRe

11:57 am on Aug 10, 2006 (gmt 0)

10+ Year Member



it works :-D

thank you!