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

我有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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值