How to use LIMIT on SUM [MySQL]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

How to use LIMIT on SUM [MySQL]

Post by MicroBoy »

Hello,

I am trying to get the sum of a specified column, but I want to limit it for 5 first rows. I have tried to use the above code but it is not working, it does not limit but it show sum for all rows. Has anyone know any other methods?

Code: Select all

$sql = mysql_query("SELECT SUM(points) FROM users WHERE u_id = $user_id LIMIT 5");
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: How to use LIMIT on SUM [MySQL]

Post by pbs »

You need to use GROUP BY clause, if you want user wise SUM(points) with LIMIT
Post Reply