sql-如果create_et日期小于特定日期,则合计价格

时间:2018-10-19 12:32:38

标签: php sql sum

我有一张付款表。表字段的名称:

id
company_id
paid
create_et (datetime)

如果company_id = company_id且create_at

我的查询:

$sql = "SELECT SUM(paid) as paid FROM payments WHERE payments.company_id = $company_id AND CAST(payments.created_at AS date)  < $specificdate";

但是它不起作用。请帮帮我。 没有错误消息,但返回null。

1 个答案:

答案 0 :(得分:1)

将您的$date变量作为日期。另外,如上面的注释中所述,您的字段列表包含create_et,但您的查询包含created_at

$sql = "SELECT SUM(paid) as paid 
        FROM payments 
        WHERE company_id = $company_id 
          AND CAST(created_at AS date)  < CAST('$specificdate' AS date)";

enter image description here