【问题标题】:how can i get the sum of all the products in a certain month in mysql如何在mysql中获得某个月份所有产品的总和
【发布时间】:2016-11-18 04:59:00
【问题描述】:

我试图在我的网站上获取特定月份的所有付款总和,比如说从 1 月到 3 月。 我的设备表有 Money 和 Joined 作为列,但 Joined 列有 date 和 time 。 如果可能的话,我想使用这种方法,但这个 sn-p 是为了奖励总和。

if (isset($_POST['moneyMade'])){ $moneyMade=0;

$query = "select SUM(Money) from users where Joined<='2016-11-01 00:00:00' and Joined>='2016-11-31 00:00:00'";
//if (!$query) { echo 'MySQL Error: ' . mysqli_error(); exit; }
//$query="select * from equipments ";
$result = mysqli_query($conn,$query);



while ($row = mysqli_fetch_assoc($result)){  /*  Fetch a result row as an associative array */
 /*while ($row = mysqli_result($result)){*/
  $moneyMade +=$row['Money'];


}

echo "Money made for this month is ". $moneyMade;

  }

如何使用 php 和一些 sql 来实现这一点? 谢谢。

【问题讨论】:

  • 显示您的查询或您已经完成的工作
  • 分享一些虚拟数据和您尝试的查询?
  • 试试这样的select SUM(amt) from tab_name where DateTime&lt;='2016-11-01 00:00:00' and DateTime&gt;=2016-11-31 00:00:00基于您的日期和时间格式的 where 条件。
  • @Karthi mysqli_fetch_assoc() 期望参数 1 是 mysqli_result,布尔值给定但给定 while ($row = mysqli_result($result)) 是未捕获错误:调用未定义函数 mysqli_result() 并打印出来本月赚的钱是 0 ,表中有大于 0 的值
  • @Blue_Jay 我认为查询有语法错误。请检查它使用此代码if (!$query) { echo 'MySQL Error: ' . mysqli_error(); exit; }

标签: php mysql oop


【解决方案1】:

假设Prize 是一个数值,您可以执行以下操作。

$month = 'January';

$month_start = strtotime("first day of {$month}");
$month_end = strtotime("last day of {$month}");

$query = "SELECT SUM(`Prize`) FROM `equipments` WHERE UNIX_TIMESTAMP(`Joined`) >= {$month_start} AND UNIX_TIMESTAMP(`Joined`) <= {$month_end}";

【讨论】:

  • OP 期望获得特定月份的数据总和。但你错过了。
  • @Jayme Brereton 我想要特定数据的结果而不是全部数据
  • @JaymeBrereton 你的方法给了我一个错误。它说有一个意外的 $result
  • 你错过了 $query 行末尾的分号
  • @JaymeBrereton 有一个错误。 mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,给定布尔值
猜你喜欢
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
相关资源
最近更新 更多