dongqianzhan8325 2015-06-23 13:59
浏览 47
已采纳

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

I started using DataTables Table plug-in for jQuery and got some problems. I am using example code from here.

I have MySQL table witch looks like that:

id | name | father_id

father_id is id value in same table only in different row. So if I want to know father name i have to search in same table WHERE id = father_id. But what DataTable does it just show the contents of MySQL table as it is.

In my DataTable i want to show data like that:

id | name | father_name | father_id

So when DataTable takes data from MySQL table, but before it creates table I want to change column value which at that time is value of father_id in the same row in MySQL. I want too add father_name by searching for it with particular father_id.

  • 写回答

3条回答 默认 最新

  • dongyi0114 2015-06-23 15:27
    关注

    As PaulF pointed out, you need to use JOIN or sub-query to retrieve father's name from the same table.

    I assume you're using ssp.class.php to process your data on the server-side based on the example you've mentioned.

    Class ssp.class.php doesn't support joins and sub-queries, but there is a workaround. The trick is to use sub-query as shown below in $table definition. Replace table with your actual table name in the sub-query.

    $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 )
    );
    

    You also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.

    Make sure all column names are unique otherwise use AS to assign an alias.

    NOTES

    There is also github.com/emran/ssp repository that contains enhanced ssp.class.php supporting JOINs.

    LINKS

    See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?