开发者

merge values in mysql and sort

开发者 https://www.devze.com 2023-02-11 00:49 出处:网络
I a mysql table like 开发者_开发百科this: CREATE TABLE vote ( `id` bigint(20) NOT NULL AUTO_INCREMENT,

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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消