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

问题描述 投票:0回答:3

我开始使用 jQuery 的 DataTables Table 插件 并遇到了一些问题。我正在使用来自here的示例代码。

我的 MySQL 表女巫看起来像这样:

id |名称 |父亲_id

father_id
是同一表中不同行中的
id
值。因此,如果我想知道父亲的名字,我必须在同一张表中搜索
WHERE id = father_id
。但 DataTable 的作用是什么,它只是显示 MySQL 表的内容。

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

id |名称 |父亲姓名 |父亲_id

因此,当 DataTable 从 MySQL 表中获取数据时,但在创建表之前,我想更改列值,当时该列值是 MySQL 中同一行中的

father_id
的值。我也想通过使用特定的
father_name
搜索来添加
father_id

php mysql datatables
3个回答
18
投票

正如PaulF指出的,您需要使用

JOIN
或子查询从同一个表中检索父亲的名字。

我假设您正在使用

ssp.class.php
根据您提到的示例在服务器端处理数据。

Class

ssp.class.php
不支持连接和子查询,但有一个解决方法。技巧是使用子查询,如下面
$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:使用 WHERE、JOIN 和 GROUP BY 与 ssp.class.php 了解更多信息。


1
投票

最近我发现自己使用 DataTables 并需要原始 ssp.class.php 不支持的更复杂的 JOIN 和 WHERE 子句。因此,我修改了原始文件并稍微更改了 API,以提供一个表格,为我提供所需的灵活性。我将“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”)子句也可能有一个“别名”来引用连接表中的列。

传递到服务器的 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

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


0
投票

错误 : “发生 SQL 错误:SQLSTATE[42000]:语法错误或访问冲突:1103 表名不正确

© www.soinside.com 2019 - 2024. All rights reserved.