ganjan
ganjan

Reputation: 7596

merge values in mysql and sort

I a mysql table like this:

CREATE TABLE vote (

    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `username` varchar(16) NOT NULL,
    `site` varchar(100) NOT NULL,
    `nr` bigint(20) NOT NULL default '1',
    `time` datetime NOT NULL default '0000-00-00 00:00:00',

    PRIMARY KEY (`id`)
) TYPE=MyISAM; 

The same user can have many entries. I want to sort by highest nr, but since the same usernamecan have many entries with different nr. Same username entries must merge and the nr values must be added together.

Upvotes: 0

Views: 172

Answers (1)

WuHoUnited
WuHoUnited

Reputation: 8429

I think you are looking for something along the lines of:

select username, sum(nr) as nrSum
 from vote
 group by username
 order by nrSum desc

Upvotes: 3

Related Questions