在复杂函数中为ssp类添加GROUP BY支持

问题描述 投票:2回答:2

我正在使用数据表v 1.10.19当我使用以下内容时,该组通过破坏分页并仅显示一页。

        $where = "recipient='".$recipient."' AND grouped='' GROUP BY id DESC";
        echo json_encode(
            SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, $where )
        );  

这是复杂的功能:

static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null )
{
    $bindings = array();
    $db = self::db( $conn );
    $localWhereResult = array();
    $localWhereAll = array();
    $whereAllSql = '';

    // Build the SQL query string from the request
    $limit = self::limit( $request, $columns );
    $order = self::order( $request, $columns );
    $where = self::filter( $request, $columns, $bindings );

    $whereResult = self::_flatten( $whereResult );
    $whereAll = self::_flatten( $whereAll );

    if ( $whereResult ) {
        $where = $where ?
            $where .' AND '.$whereResult :
            'WHERE '.$whereResult;
    }

    if ( $whereAll ) {
        $where = $where ?
            $where .' AND '.$whereAll :
            'WHERE '.$whereAll;

        $whereAllSql = 'WHERE '.$whereAll;
    }

    // Main query to actually get the data
    $data = self::sql_exec( $db, $bindings,
        "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
         FROM `$table`
         $where
         $order
         $limit "
    );

    // Data set length after filtering
    $resFilterLength = self::sql_exec( $db, $bindings,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table`
         $where"
    );
    if(empty($resFilterLength)){$recordsFiltered="['1','2']";}else{
    $recordsFiltered = $resFilterLength[0][0];
    }       
    //$recordsFiltered = $resFilterLength[0][0];

    // Total data set length
    $resTotalLength = self::sql_exec( $db, $bindings,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table` ".
        $whereAllSql
    );
    if(empty($resTotalLength)){$recordsTotal="['1','2']";}else{
    $recordsTotal = $resTotalLength[0][0];
    }           
    //$recordsTotal = $resTotalLength[0][0];

    /*
     * Output
     */
    return array(
        "draw"            => isset ( $request['draw'] ) ?
            intval( $request['draw'] ) :
            0,
        "recordsTotal"    => intval( $recordsTotal ),
        "recordsFiltered" => intval( $recordsFiltered ),
        "data"            => self::data_output( $columns, $data )
    );
}

问题是应该添加/更改哪些内容以添加对GROUP BY子句的支持但是,我可以使用以下属性的数据表来显示GROUP BY DESC但是如果它是服务器端则会更好:

'order': [4, 'desc'],

更新:

由@scaisEdge建议:

对于第一个建议我改变了以下内容:

    // Data set length after filtering
    $resFilterLength = self::sql_exec( $db, $bindings,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table`
         $where"
    );

    // Data set length after filtering
    $resFilterLength = self::sql_exec( $db, $bindings,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table`".
         $where
    );

第二个建议:

从ssp :: complex json编码语句中删除了GROUP BY子句

    // Main query to actually get the data
    $data = self::sql_exec( $db, $bindings,
        "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
         FROM `$table`
         $where
         $order
         $limit "
    );

// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
    "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
     FROM `$table`
     $where GROUP BY id DESC
     $order
     $limit "
);

完美的工作:)

php mysql pdo group-by datatables
2个回答
2
投票

两个建议

1)在这段代码中你应该使用字符串连接$ where

$resFilterLength = self::sql_exec( $db, $bindings,
    "SELECT COUNT(`{$primaryKey}`)
     FROM   `$table` " .   $where 
);

2)似乎你有按组的限制和偏移顺序排序(和ID DESC组错误)

   $where = "recipient='".$recipient."' AND grouped=''  
    GROUP BY id ORDER BY id DESC LIMIT 10 OFFSET 5";

0
投票

经过上述更改后,可以通过ssp :: complex()语句轻松传递group by子句或任何其他子句,如下所示:

        $where = "recipient='".$recipient."' AND grouped=''";
        $extra ='GROUP BY id DESC';
        echo json_encode(
            SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns, null, $where, $extra )
        );

在SSP.CLASS.PHP的复杂功能中进行更改:

static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null)

static function complex ( $request, $conn, $table, $primaryKey, $columns, $whereResult=null, $whereAll=null, $extra=null )

// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
    "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
     FROM `$table`
     $where
     $order
     $limit "
);

// Main query to actually get the data
$data = self::sql_exec( $db, $bindings,
    "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
     FROM `$table`
     $where $extra
     $order
     $limit "
);
© www.soinside.com 2019 - 2024. All rights reserved.