[UPHPU] MySQL query to get totals
Morgan Terry
localstatic at localstatic.com
Sun Sep 28 10:14:07 MDT 2008
On Sun, Sep 28, 2008 at 10:02 AM, Wade Preston Shearer
<lists at wadeshearer.com> wrote:
> I have an MySQL table that looks like this:
>
> primary key | ID | date
> ----------+--+------------
> 1 | 1 | 2008-09-09
> 2 | 1 | 2008-09-10
> 3 | 2 | 2008-09-09
> 4 | 2 | 2008-09-13
>
>
> …where ID is a reference to something that happens multiple times.
>
> I want to to display the content of the table like this:
>
> ID 1: (sum of all rows with ID 1)
> ID 2: (sum of all rows with ID 2)
> ID 3: (sum of all rows with ID 3)
>
>
> …or exactly how it would look…
>
> 1: 5
> 2: 2
> 3: 16
>
>
> …etc.
>
>
> I can do this by performing a query for each ID, but would like to know if
> it is possible to retrieve all of the data with a single query.
It sure is. Something like this should work:
SELECT primary_key, sum(id)
FROM table_name
GROUP BY primary_key
More information about the UPHPU
mailing list