如何使用 ssp.class.php 连接两个表

How to join two tables with ssp.class.php

我开始使用 DataTables Table plug-in for jQuery and got some problems. I am using example code from here

我有 MySQL table 看起来是这样的:

id | name | father_id

father_id 是相同 table 中的 id 值,仅在不同的行中。所以如果我想知道父亲的名字,我必须在相同的 table WHERE id = father_id 中搜索。但是DataTable是干什么的,它只是把MySQLtable的内容原样显示出来。

在我的 DataTable 中,我想显示这样的数据:

id | name | father_name | father_id

因此,当 DataTable 从 MySQL table 获取数据时,但在创建 table 之前,我想更改列值,该值当时是 father_id 的值MySQL 中的同一行。我也想通过使用特定的 father_id.

搜索来添加 father_name

加入 table 本身 - 您将需要使用别名

SELECT a.id, a.name, b.name, b.id
FROM table a
join table b on (b.father_id=a.id);

正如 所指出的,您需要使用 JOIN 或子查询从同一个 table.

中检索父亲的姓名

我假设您正在使用 ssp.class.php 根据您提到的示例在服务器端处理您的数据。

Class ssp.class.php 不支持连接和子查询,但有一个解决方法。诀窍是使用子查询,如下面的 $table 定义所示。将 table 替换为您在子查询中的实际 table 名称。

$table = <<<EOT
 (
    SELECT 
      a.id, 
      a.name, 
      a.father_id, 
      b.name AS father_name
    FROM table a
    LEFT JOIN table b ON a.father_id = b.id
 ) temp
EOT;

$primaryKey = 'id';

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

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

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

您还需要编辑 ssp.class.php 并将 FROM `$table` 的所有实例替换为 FROM $table 以删除反引号。

确保所有列名都是唯一的,否则使用 AS 分配别名。

注意事项

还有 github.com/emran/ssp 存储库,其中包含增强的 ssp.class.php 支持 JOIN。

链接

有关详细信息,请参阅 jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php

最近我发现自己在使用 DataTables 并需要更复杂的 JOIN 和 WHERE 子句,而原始 ssp.class.php 不支持这些子句。因此,我修改了原始文件并稍微更改了 API 以提供 table,这为我提供了所需的灵活性。我将 "SSP::simple" 和 "SSP::complex" 的功能合并到一个名为 "SSP::process".

的函数中

由于脚本的长度,我把它放在pastebin.com这里:ssp.class.php

以及我如何使用它的简单示例:

private function get_recent_payments() {
    global 
        $pdoHost, $pdoUser, 
        $pdoPass, $pdoDatabase;

    // SQL server connection information
    $sql_details = array(
        'user' => $pdoUser,
        'pass' => $pdoPass,
        'db'   => $pdoDatabase,
        'host' => $pdoHost
    );

    // DataTables server-side processing
    require_once('ssp.class.php');

    $options = [
        'table' => 'payments',
        'alias' => 'l',
        'primaryKey' => 'id',
        'columns' => [
            [ 'db' => 'id',       'dt' => 0 ],
            [
                'db' => 'client_id',     
                'dt' => 1,
                'join' => [
                    'table' => 'clients',
                    'on' => 'id',
                    'select' => 'first_name',
                    'alias' => 'c',
                    'as' => 'fname',
                ]
            ],
            [
                'db' => 'client_id',     
                'dt' => 2,
                'join' => [
                    'table' => 'clients',
                    'on' => 'id',
                    'select' => 'last_name',
                    'alias' => 'c'
                ]
            ],
            [ 'db' => 'pay_date', 'dt' => 3 ]
        ],
        'where' => [
            [
                'db' => 'client_id',
                'op' => '!=',
                'value' => $_SESSION['client_id']
            ]
        ]
    ];

    $this->response(SSP::process($_REQUEST, $sql_details, $options));
}

选项数组的 'where' 和 'whereResult'(详见 'SSP::complex')子句也可以有一个 'alias' 来引用连接的 table.

示例SQL 传递给服务器的查询:

SELECT l.`id`, c.`first_name` AS 'fname', c.`last_name`, l.`pay_date` 
        FROM `payments` l
        JOIN `clients` c ON (c.`id` = l.`client_id`)
        WHERE l.`client_id` != :binding_0
        ORDER BY l.`pay_date` DESC
        LIMIT 0, 5

我采用了结构化数组路线,因为这使我能够构建查询,同时保持带有反引号和绑定语句参数的查询的刚性。我提出这个 post 希望其他人会发现它和我一样有用。