今天罗浮宫群的一个同学问了个成绩单的问题。
以前我也遇到过,当时记得费了好大劲。现在正好回味一番。
数据库结构如下。数据插入语句如下。
CREATE TABLE IF NOT EXISTS `chengji` (
`goods_id` int(5) NOT NULL,
`user_id` int(5) NOT NULL,
`point` int(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- 转存表中的数据 `chengji`
--
INSERT INTO `chengji` (`goods_id`, `user_id`, `point`) VALUES
(100, 10, 5),
(200, 10, 8),
(300, 10, 20),
(100, 11, 5);
在空库中执行后你会得到如下的结构。
要求:查询每个用户的积分和并按照和排序
SELECT user_id, SUM( POINT ) as pertotal
FROM chengji group by user_id order by pertotal desc