how to use group by clause in ssp.class.php of datatables

拈花ヽ惹草 提交于 2020-01-25 13:08:48

问题


The sql should be

select max(id),Staff_name from position group by Staff_name

I modify the ssp.class.php.

SELECT SQL_CALC_FOUND_ROWS ".implode(", ", self::pluck($columns, 'db'))."
FROM $table
$where
$order
$limit group by Staff_name.

However, it dose not work. How to realize this sql?


回答1:


SOLUTION

Class ssp.class.php doesn't support JOIN, GROUP BY or sub-queries, but there is a workaround. The trick is to use sub-query as shown below in $table definition in you server-side processing script (server_processing.php).

For example:

$table = <<<EOT
 (
    SELECT 
      MAX(id),
      Staff_name
    FROM position 
    GROUP BY Staff_name
 ) temp
EOT;

$primaryKey = 'id';

$columns = array(
   array( 'db' => 'id', 'dt' => 0 ),
   array( 'db' => 'Staff_name',  'dt' => 1 )
);

$sql_details = array(
   'user' => '',
   'pass' => '',
   'db'   => '',
   'host' => ''
);

require( 'ssp.class.php' );
echo json_encode(
   SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

You also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.

NOTES

There is also github.com/emran/ssp repository that contains enhanced ssp.class.php supporting JOIN and GROUP BY.

LINKS

See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.



来源:https://stackoverflow.com/questions/39969867/how-to-use-group-by-clause-in-ssp-class-php-of-datatables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!