【问题标题】:How join two table or writing custom queries in Datatable php?如何在 Datatable php 中连接两个表或编写自定义查询?
【发布时间】:2016-04-18 09:39:30
【问题描述】:

我正在使用数据表将数据列为服务器端脚本。

    // DB table to use
$table = 'enquiry';

// Table's primary key
$primaryKey = 'enquiry_id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array(
        'db' => 'enquiry_date',
        'dt' => 0,
        'formatter' => function( $d, $row ) {
            return date('jS M y', strtotime($d));
        }
    ),
    array('db' => 'parent_name', 'dt' => 1),
    array('db' => 'child_name', 'dt' => 2),
    array('db' => 'mobile', 'dt' => 3),
    array('db' => 'emirate', 'dt' => 4),
    array('db' => 'remarks', 'dt' => 5),
    array('db' => 'enquiry_status_id', 'dt' => 6),
);

// SQL server connection information
$sql_details = array(
    'user' => 'root',
    'pass' => '',
    'db' => 'lsn',
    'host' => ''
);


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( './server_side/scripts/ssp.class.php' );

echo json_encode(
        SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns)
);

这是我的 php 服务器端脚本,在单个表中工作得很好。

我想在列出表格行的同时加入两个表格。但我找不到任何加入表格或编写自定义查询的选项。

如果可能,我如何加入表格?

【问题讨论】:

    标签: php mysql join datatable


    【解决方案1】:

    第一步:打开ssp.class.php

    第 2 步:找到这一行..... FROM '$table'

    第 3 步:删除单引号.... FROM $table

    第4步:对另一个做同样的事情..... FROM '$table' "

                                         FROM    $table"
    

    第 5 步:您现在可以向 $table 添加子查询

    example $table="(select * from my_list join...)";

    第 6 步:庆祝 =)

    【讨论】:

      【解决方案2】:

      这里两个表keyid相同的字段和其他字段fetch

      $sql_t="SELECT tabkey.keyid, table1.value, table2.value
      FROM
      (SELECT table1.keyid FROM table1
      UNION
      SELECT table2.keyid FROM table2) as tabkey
      LEFT JOIN
      table1 on tabkey.keyid = table1.keyid
      LEFT JOIN
      table2 on tabkey.keyid = table2.keyid;";
      

      【讨论】:

        猜你喜欢
        • 2016-10-16
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多