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

2019-09-06 10:06发布

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条回答
叼着烟拽天下
2楼-- · 2019-09-06 10:53

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.

查看更多
登录 后发表回答