【问题标题】:Select sum of total where the date lies between two dates does not get displayed选择不显示日期位于两个日期之间的总和
【发布时间】:2018-12-07 16:28:57
【问题描述】:

我正在尝试进行查询,它应该给我列名表的总和,其中时间戳位于两个日期之间。

这是我的 SQL 查询:

$year_start = strtotime('2018-01-01');
$year_end = strtotime('2018-12-31 23:59:59.000');

$query = "SELECT SUM(total) FROM buchungen WHERE datum>='".$year_start."' AND 
datum<='".$year_end."'";

然后我尝试像这样回显结果:

while($row = mysqli_fetch_assoc($result)){
echo $row["total"];

【问题讨论】:

  • 请分享 buchungen 表的数据结构。
  • 您如何在此表中存储日期?作为日期或字符串?

标签: php sql date select


【解决方案1】:

您必须将日期格式化为 sql 格式 yyyy-mm-dd hh:mm:ss。

有点像

$year_start = date("YYYY-mm-dd", strtotime('2018-01-01'));

【讨论】:

    【解决方案2】:

    查询要求日期以文本形式显示

    $year_start = '2018-01-01';
    $year_end = '2018-12-31 23:59:59.000;
    
    $query = "SELECT SUM(total) 
              FROM buchungen 
              WHERE datum>='$year_start' 
              AND datum<='$year_end'";
    

    此外,在您的测试中,没有名为 total 的列,默认情况下该列将称为 SUM(total),除非您像这样重命名该列

    $year_start = '2018-01-01';
    $year_end = '2018-12-31 23:59:59.000;
    
    $query = "SELECT SUM(total) as total 
              FROM buchungen 
              WHERE datum>='$year_start' 
              AND datum<='$year_end'";
    

    【讨论】:

    • ..和$row["SUM(total)"]?
    • @JustBaron 啊,谢谢,当时正在编辑
    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2020-05-01
    相关资源
    最近更新 更多