如何使用服务器端处理DataTables加入2个表

时间:2018-02-12 06:36:42

标签: php sql join datatable

Index.php HTML



$(document).ready(function() {
  $('#users').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "server_processing.php"
  });
});

<head>
  <title>Server Side DataTable</title>

  <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet" type="text/css">
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <link href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>
  <table id="users" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>Id</th>
        <th>Fist Name</th>
        <th>Last Name</th>
        <th>Phone</th>
        <th>Location</th>
      </tr>
    </thead>
  </table>
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>
&#13;
&#13;
&#13;

服务器脚本

$table = 'users';
$primaryKey = 'id';


$columns = array(
    array( 'db' => 'id',          'dt' => 0 ),
    array( 'db' => 'first_name',        'dt' => 1 ),
    array( 'db' => 'last_name',     'dt' => 2 ),
    array( 'db' => 'phone',    'dt' => 3 ),
    array( 'db' => 'site',    'dt' => 4 ),
);


// SQL server connection information
$sql_details = array(
    'user' => 'root',
    'pass' => '',
    'db'   => 'datatables',
    'host' => 'localhost'
);

  require( 'ssp.class.php' );

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

  );

数据库表

用户

id| first_name| last_name| phone| site|
--- ---------- ---------- ------ ------
1   Barry       Roux      0454554   1
2   Sylvester   Gus       012124    2

位点

id| name
--- ---------- 
1   London    
2   Edinburgh

期望输出

ids| name| surname |     number|    location
--- ------   ----         -------   ------
1   Barry       Roux      0454554   London
2   Sylvester   Gus       012124    Edinburgh

上面的代码显示了如何使用服务器端数据表从一个表(用户)获取记录的基本示例。
现在我想加入并显示2个表中的记录。使用类似这样的东西查询:

SELECT u.id as ids, u.first_name as name , u.last_name as surname, u.phone as number , s.name as location
FROM users u
LEFT JOIN sites s ON s.id = u.site.

知道我该怎么做吗?

请帮忙

3 个答案:

答案 0 :(得分:0)

您现在可能已经解决了...但是(像我一样)其他人可能遇到了这个问题,我找到了解决方法!

这里是:https://www.gyrocode.com/articles/jquery-datatables-using-where-join-and-group-by-with-ssp-class-php/

您将需要编辑ssp.class.php。找到并替换所有的,然后将FROM`$ table'替换为FROM $ table(删除反引号)。

<?php

$table = <<<EOT
 (
    SELECT
      a.id,
      a.name,
      b.position
    FROM table a
    LEFT JOIN positions b ON a.position_id = b.id
 ) temp
EOT;

$primaryKey = 'id';

$columns = array(
   array( 'db' => 'id',          'dt' => 0 ),
   array( 'db' => 'name',        'dt' => 1 ),
   array( 'db' => 'position',    'dt' => 2 )
);

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

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

所有荣誉归功于Michael Ryvkin。

答案 1 :(得分:0)

在mysql中创建视图,并在$ table变量中使用视图名称,例如 $ table ='users_data_view';在您的服务器脚本中,即ajax_json.php文件

视图结构 CREATE VIEW users_data_view AS select_join_query_goes_here

在mysql中,在sql命令下运行此命令(并将“ select_join_query_goes_here”替换为您的选择查询)以创建新视图

CREATE VIEW users_data_view AS SELECT u.id as ids, u.first_name as name , u.last_name as surname, u.phone as number , s.name as location
FROM users u
LEFT JOIN sites s ON s.id = u.site

答案 2 :(得分:0)

考虑使用https://github.com/emran/ssp作为ssp.class.php的替代方法。我个人发现很难在连接表中使用ssp.class.php。