在DataTables服务器端分组

时间:2016-09-22 07:08:28

标签: jquery codeigniter datatables

我使用了datatables服务器端。但是当我的查询需要分组来选择数据时,我遇到了问题。如何在datatables服务器端分组?这可能吗?

1 个答案:

答案 0 :(得分:0)

是的,有可能。但你需要使用" group by"作为子查询。否则,分页/数据计数将被搞砸。

SELECT category_name, total_length
FROM (Select 
  any_value(category.name) as category_name, 
  concat(sum(length), \" minutes\") as total_length 
from film 
left join film_category on film_category.film_id = film.film_id
left join category on film_category.category_id = category.category_id
group by category.category_id) as Table1

如果您不想浪费时间编码,请查看此链接。这是一个如何使用datatables库的例子。

https://datatables.ozdemir.be/group-by

<?php
require '../../../vendor/autoload.php';
use Ozdemir\Datatables\Datatables;
use Ozdemir\Datatables\DB\MySQL;
include "../../_config.php";
$dt = new Datatables(new MySQL($config));
$dt->query("Select 
  any_value(category.name) as category_name, 
  concat(sum(length), \" minutes\") as total_length 
from film 
left join film_category on film_category.film_id = film.film_id
left join category on film_category.category_id = category.category_id
group by category.category_id");
echo $dt->generate();