添加同一日期的值 [英] Add values from same date

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

问题描述

我刚刚开始学习PHP,因此,如果这个问题已经有了答案,请指向我,因为我找不到它.

I just got started learning PHP so if there's already an answer to this question, please point me to it as I wasn't able to find one.

我有一个带有2个条目的mysqli数据库:date_added& menu_price.当日期以以下格式自动设置时,用户(通过网页界面)输入menu_price(双精度).请注意,可以为同一日期设置menu_price的多个条目.

I have a mysqli database with 2 entries: date_added & menu_price. The user inputs (through a webpage interface) the menu_price (double) while the date is automatically set in this format: Y-m-d. Note that multiple entries for menu_price can be set for the same date.

我的问题:我正在尝试提取一份每日报告,该报告应添加每天的所有价格,并且只显示一次日期.

My question: I'm trying to pull out a daily report which should add all prices for every day and only display the date once.

示例:假设我有两个2015-04-10条目:10& 20,而我有3个2015-04-11条目:10、20& 30.因此,我对2015-04-10的报告应为30,对于2015-04-11的报告应为60.

Example: let's say I had two entries for 2015-04-10: 10 & 20 and I had 3 entries for 2015-04-11: 10, 20 & 30. So my report for 2015-04-10 should say 30 and for 2015-04-11 should say 60.

我设法按以下方法提取唯一的日期条目(不确定它是否有用),但不确定如何进行.

I managed to extract unique date entries (I'm not sure it's useful or not) the way described below but I'm not sure how to proceed.

// Extract unique date entries
$newDate = "SELECT DISTINCT (date_added) AS date_added
            FROM main
            ORDER BY date_added DESC";

$dateResult = $conn->query($newDate);

// Create an array with unique dates
$results = array();
while ($dateArray = $dateResult->fetch_assoc())
{
    $results[] = $dateArray["date_added"];
};

推荐答案

这需要聚合函数.使用 SUM() 在您的查询中:

This requires aggregate functions. Use SUM() and GROUP BY in your query:

$newDate = "SELECT date_added,
                SUM(menu_price )
            FROM main
            GROUP BY date_added
            ORDER BY date_added DESC";

这篇关于添加同一日期的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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