行&总和列

发布于 2024-08-18 01:21:07 字数 102 浏览 8 评论 0原文

我有一个非常复杂的查询(mysql + php),我想知道除了使用 SUM(x) 之外对表的列和行求和的最简单方法。

也许 JavaScript 可以提供帮助。 提前致谢。

i have a really complex query (mysql + php) and i would like to know the easiest way to sum columns and rows of a table apart from using SUM(x).

Maybe a javascript could help.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2024-08-25 01:21:07

我建议使用 MySQL 中的 SUM() 函数。不使用它的唯一原因是如果您有一些基于其他值的复杂计数。然后我会在 PHP 中进行计数。这是一个例子:

$result = mysql_query("... query here ...");
$cats = 0;
$dogs = 0;
while($row = mysql_fetch_array($result))
{
     if($row['type'] == 'cat')
     {
          $cats++;
     }
     else
     {
          $dogs++;
     }
 }
echo "Cats: $cats Dogs: $dogs";

I would advise to use the SUM() function in MySQL. The only reason to not use it is if you have some complicated counting based on other values. Then I would do the counting in the PHP. Here is an example:

$result = mysql_query("... query here ...");
$cats = 0;
$dogs = 0;
while($row = mysql_fetch_array($result))
{
     if($row['type'] == 'cat')
     {
          $cats++;
     }
     else
     {
          $dogs++;
     }
 }
echo "Cats: $cats Dogs: $dogs";
旧伤还要旧人安 2024-08-25 01:21:07

我会在查询本身中执行此操作。如果您发布查询,我可以提供更多信息。

I would do this in the query itself. If you post the query, I can provide more information.

甜点 2024-08-25 01:21:07

这是查询,我想对列和行求和。 (行是一年中每个月订购的变量“svago”和“lavoro”,而列是全年的相同值)

$q = "SELECT DISTINCT DATE_FORMAT( TIMESTAMP,  '%m%Y' ) AS derp FROM main LIMIT 0 , 30";
$qq = mysql_query($q);

while($res = mysql_fetch_array($qq)) {

$where = $res['derp'];

$q = "SELECT timestamp, SUM(moto) + SUM(mary) AS svago, SUM(lavoro) + SUM(affitto) AS lavoro FROM main WHERE DATE_FORMAT(timestamp, '%m%Y') = $where";

This is the query, i want to sum cols and rows. ( rows are the vars "svago" and "lavoro" ordered for each month of the year, while cols are the same values in the whole year )

$q = "SELECT DISTINCT DATE_FORMAT( TIMESTAMP,  '%m%Y' ) AS derp FROM main LIMIT 0 , 30";
$qq = mysql_query($q);

while($res = mysql_fetch_array($qq)) {

$where = $res['derp'];

$q = "SELECT timestamp, SUM(moto) + SUM(mary) AS svago, SUM(lavoro) + SUM(affitto) AS lavoro FROM main WHERE DATE_FORMAT(timestamp, '%m%Y') = $where";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文