2016-09-22 89 views
0

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

回答

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 

如果你不想浪费你的时间与编码,请检查此链接。这是一个如何使用数据表库来实现的例子。

https://github.com/n1crack/datatables-examples/tree/master/sqlite_examples/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();