在MYSQL中计算一个但低于给定id的值

时间:2014-03-05 10:25:28

标签: mysql

如果用户想要通过传递id 4来获取总user_points,那么user_point将是4,因为id 1,2,3,4 total user_point是4

因此,如果用户希望通过传递id 6来获取total_points,则总user_point将为6,因为id 1,2,3,4,5,6 total user_points为6

CREATE TABLE IF NOT EXISTS `amount` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned DEFAULT NULL,
  `user_point` smallint(1) DEFAULT '0',
  `total` float(6,2) DEFAULT NULL,
  `paid` float(6,2) DEFAULT NULL,
  PRIMARY KEY (`id`)  
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;


INSERT INTO `amount` (`id`, `user_id`, `user_point`, `total`, `paid`) VALUES
(1, 29, 1, 44.00, 44.00),
(2, 29, 1, 125.4, 125.40),
(3, 29, 1, 95.00, 95.00),
(4, 29, 1, 44.00, 44.00),
(5, 29, 1, 94.00, 94.00),
(6, 29, 1, 50.00, 50.00);

请帮帮我

4 个答案:

答案 0 :(得分:0)

试试这个:

SELECT COUNT(id) total FROM amount 
WHERE user_id= <userid>;

答案 1 :(得分:0)

select count(ID) as count from amount where user_id = <ID>

尝试此查询

答案 2 :(得分:0)

select sum(user_point) as tot_point from amount where user_id='29';

答案 3 :(得分:0)

尝试此查询,

SELECT SUM(`user_point`) AS tot_points FROM `amount`
WHERE `user_id`= <userid>;