我有MySQL表女巫看起来像那样:
id | name | father_id
father_id只是在同一个表中的id值,只在不同的行中.因此,如果我想知道父亲的名字,我必须在同一个表中搜索WHERE id = father_id.但DataTable所做的只是显示MySQL表的内容.
在我的DataTable中,我希望显示如下数据:
id | name | father_name | father_id
因此,当DataTable从MySQL表中获取数据时,但在创建表之前,我想要更改当时在MySQL中同一行中的father_id值的列值.我想通过使用特定的father_id搜索来添加father_name.
解决方法:
正如PaulF所指出的,您需要使用JOIN或子查询从同一个表中检索父亲的名字.
我假设您正在使用ssp.class.php根据您提到的示例在服务器端处理您的数据.
类ssp.class.php不支持连接和子查询,但有一种解决方法.诀窍是在$table definition中使用如下所示的子查询.将表替换为子查询中的实际表名.
$table = <<
(
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存储库,其中包含支持JOIN的增强型ssp.class.php.
链接
标签:php,mysql,datatables
来源: https://codeday.me/bug/20190612/1222344.html