Jump to content

datatables and SUM


An7hony
Go to solution Solved by An7hony,

Recommended Posts

I am using datatables.net and want to include a SUM() on this query below:

 

$sQuery = "
SELECT SQL_CALC_FOUND_ROWS * FROM
(SELECT p.people_id, p.people_firstName, p.people_surname, p.people_address, p.people_town, p.people_county, p.people_postcode, SUM(r.relationships_id)
FROM People p LEFT JOIN Relationships r ON p.people_id = r.people_id) sel
$sWhere
$sOrder
$sLimit
";

 

This is only returning one row (should return around 2k records in the database)

 

I have tried adding GROUP BY

 

        $sQuery = "
SELECT SQL_CALC_FOUND_ROWS * FROM
(SELECT p.people_id, p.people_firstName, p.people_surname, p.people_address, p.people_town, p.people_county, p.people_postcode, SUM(r.relationships_id)
FROM People p LEFT JOIN Relationships r ON p.people_id = r.people_id GROUP BY r.relationships_id) sel
$sWhere
$sOrder  
$sLimit
";

 

Although this is throwing a JSON format error

 

Below is my full code:

<?php
    
    
    $aColumns = array('people_id', 'people_firstName', 'people_surname', 'people_address', 'people_town', 'people_county', 'people_postcode', 'relationships_id', 'people_id');

    
    /* Indexed column (used for fast and accurate table cardinality) */
    $sIndexColumn = "people_id";
    
    /* DB table to use */
    $sTable = "People";
    
    
    
    /*
     * Paging
     */
    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
        $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
            intval( $_GET['iDisplayLength'] );
    }
    
    
    /*
     * Ordering
     */
    $sOrder = "";
    if ( isset( $_GET['iSortCol_0'] ) )
    {
        $sOrder = "ORDER BY ";
        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
        {
            if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
            {
                $sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
                    ($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
            }
        }
        
        $sOrder = substr_replace( $sOrder, "", -2 );
        if ( $sOrder == "ORDER BY" )
        {
            $sOrder = "";
        }
    }
    
    
    /*
     * Filtering
     * NOTE this does not match the built-in DataTables filtering which does it
     * word by word on any field. It's possible to do here, but concerned about efficiency
     * on very large tables, and MySQL's regex functionality is very limited
     */
    $sWhere = "";
    if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
    {
        $sWhere = "WHERE (";
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
        }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ')';
    }
    
    /* Individual column filtering */
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
        {
            if ( $sWhere == "" )
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
        }
    }

        
        $sQuery = "
SELECT SQL_CALC_FOUND_ROWS * FROM
(SELECT p.people_id, p.people_firstName, p.people_surname, p.people_address, p.people_town, p.people_county, p.people_postcode, r.relationships_id
FROM People p LEFT JOIN Relationships r ON p.people_id = r.people_id) sel
$sWhere
$sOrder
$sLimit
";
    // http://datatables.net/forums/discussion/2774/sql-join-two-tables/p1    
        
    $rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    
    /* Data set length after filtering */
    $sQuery = "
        SELECT FOUND_ROWS()
    ";
    $rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
    $iFilteredTotal = $aResultFilterTotal[0];
    
    /* Total data set length */
    $sQuery = "
        SELECT COUNT(`".$sIndexColumn."`)
        FROM $sTable
    ";
    $rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
    $aResultTotal = mysql_fetch_array($rResultTotal);
    $iTotal = $aResultTotal[0];
    
    
    /*
     * Output
     */
    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );
    
    
    
    while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $aColumns[$i] == "relationships_id" )
            {
                
                
                /* Special output formatting for 'version' column */
                $row[] = ($aRow[ $aColumns[$i] ]=="") ? '0' : $aRow[ $aColumns[$i] ];
            }
            else if ( $aColumns[$i] != ' ' )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }
    
    echo json_encode( $output );
?>


Can any one help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.