DataTables 服务器端:使用 GROUP BY 和 SUM 进行查询



我正在使用DataTables服务器端来显示我的数据。(示例( (手动(。

我能够通过以下方式扩展此工具:

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $groupby )
);

在类 SSP 中.class.php

$data = self::sql_exec( $db, $bindings,
    "SELECT SQL_CALC_FOUND_ROWS `".implode("`, `", self::pluck($columns, 'db'))."`
     FROM `$table`
     $where
     $groupby
     $order
     $limit"
);

但是当我想要一列的总和时,我收到一个错误,即找不到列 SUM(毛价格(。

以下是列的处理方式:

$columns = array(
    array( 'db' => 'ProductDescription',  'dt' => 0 ),
    array( 'db' => 'SUM(GrossPrice)',   'dt' => 1 ),
    array( 'db' => 'SUM(Number)',   'dt' => 2 )
);

老问题,但我回答谁有同样的问题。

可以将此类与 joinQuery 一起使用。我们使用joinQuery,因为如果我们不使用,SSP类将使用自己的查询。

$columns = array(
    array('db' => 'x.ProductDescription', 'dt' => 0, 'field' => 'ProductDescription'),
    array('db' => 'SUM(x.GrossPrice) as GrossPrice', 'dt' => 1, 'field' => 'GrossPrice'),
    array('db' => 'SUM(x.Number) as Number', 'dt' => 2, 'field' => 'Number')
);

joinQuery 示例:

$joinQuery = "FROM {$table} AS x";

并像这样打电话:

echo json_encode(
   SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, $joinQuery)
);

为JOIN查询自定义的此类,但您可以像这样使用。感谢埃姆兰的这堂课。

我找到了一个解决方法:

我在mysql中使用"sum(("查询创建了一个视图,并且只使用数据表来显示此视图。

希望这能为其他人服务。

最新更新