具有相同值的javascript sum行 [英] javascript sum row that has same values

查看:69
本文介绍了具有相同值的javascript sum行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自动增量列的表,第二个带有从1到5的随机数。



喜欢:

I have a table with a column of id auto increment and the second with random numbers from 1 to 5.

Like:

id | number
1     4
2     5
3     3
4     4
5     1
6     3
7     4
...   ...





td看起来像



the td looks like that

<td>1</td><td id="p4">4</td>
<td>2</td><td id="p5">5</td>



我要总结所有相同的数字显示在我的第二个表格中,如:


I want to sum all the same numbers to display it into my second table, like:

5 | 4 | 3 | 2 | 1
4  12   6   0   1







$(document).ready(function(){
var p1 = document.getElementById("p1").innerHTML;
// sum the number of p1 we have in to table
});



有什么想法吗?


Any idea?

推荐答案

document )。ready( function (){
var p1 = < span class =code-sdkkeyword> document .getElementById( p1)。 innerHTML;
// 将表格中的p1数加起来
});
(document).ready(function(){ var p1 = document.getElementById("p1").innerHTML; // sum the number of p1 we have in to table });



有什么想法?


Any idea?


尽管这种方法不可行,但您应该始终只在数据库级别执行所需的数据操作(在您的情况下使用MySQL)。在MySQL中搜索GROUP BY子句并使用SUM()函数。查询看起来像 -

Even though this approach is not advisable, you should always peform required data manipulation at the database level only (using MySQL in your case). Search for GROUP BY clause and use of SUM() function in MySQL. The query will look something like -
SELECT SCORE, SUM(SCORE) TOTAL FROM (SELECT ID, SCORE FROM MYTABLE) GROUP BY SCORE;



无论如何,对于JavaScript解决方案,查看此链接 -

http://jsbin.com/efacem/1/ [ ^ ]


这篇关于具有相同值的javascript sum行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆