SELECT SUM与日期变量,用于prestashop

时间:2013-11-20 14:32:15

标签: php mysql select sum prestashop

我想在PHP / MYSQL上得到一个结果 我有一张表ps_orders,其价格为total_paid

我需要在当前日期询问所有价格的总额,我很难插入正确的日期。我要停在这里,不要工作...... thnaks

....

$date = date("Y-m-d");

$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '%$date%'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo mysql_result($result, 0);

3 个答案:

答案 0 :(得分:0)

尝试

$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date = '$date'";

$query = "SELECT SUM(total_paid) FROM ps_orders WHERE delivery_date LIKE '%$date%'";

答案 1 :(得分:0)

取决于您的delivery_date字段的设置方式以及您在$ date变量中使用的内容(时间戳vs只是mdy),查询到where子句看起来没问题,但您也可以尝试:

SELECT SUM(total_paid)
FROM ps_orders
WHERE delivery_date = $date;

如果您正在使用delevery_date的日期时间字段,则必须更深入地使用范围:

SELECT SUM(total_paid)
FROM ps_orders
where (delivery_date > $date
and deliver_date < $date +interval 1 day)

使用date http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

时,此链接也可以帮助您

答案 2 :(得分:0)

您可以使用以下其中一项:

  1. mysql中的DATE函数会将2013-11-20 10:54:12转换为2013-11-20,即截断日期时间

    $ query =“SELECT SUM(total_paid)FROM ps_orders WHERE DATE(delivery_date)='$ date'”;

  2. 或者在$ date之后只使用一个'%'并使用LIKE,因此此值将与2013-11-20 10:12:13之类的日期匹配:

    $ query =“SELECT SUM(total_paid)FROM ps_orders WHERE delivery_date like'$ date%'”;

  3. 或使用字符串mysql函数SUBSTRING

    $ query =“SELECT SUM(total_paid)FROM ps_orders WHERE SUBSTRING(delivery_date1,1,10)='$ date'”;

  4. 使用datetime with time和BETWEEN mysql比较运算符:

    $ query =“SELECT SUM(total_paid)FROM ps_orders WHERE delivery_date1在'$ date 00:00:00'和'$ date 23:59:59'之间