PHP - Return sum of columns at specified date // PDO

0

>

For example: A person who sees the sum of the integers of these two columns for 2016-09-20 . How to do this script? ( PDO )

In case he should return the sum of 25.5 ...

Thegeneralreturn,howevernotsummedandtbmseparate...

$bsc_user=$pdo->prepare("SELECT * FROM 'tab_newprodut' WHERE 'p_data'=?");
        $bsc_user->execute(array("2016-09-20"));
        $bsc_cont = $bsc_user->rowCount();
        if($bsc_cont > 0){
            while($linha = $bsc_user->fetch(PDO::FETCH_ASSOC)) {
                echo $linha['p_line_a'].' '.$linha['p_line_l'].'<br>';
            }
        } else {
            echo '<div class="return_box-list-u">Nenhum registro localizado.</div>';
        }

1 - It needs to total the two columns p_line_a, p_line_l that refer to the specified date.
2 - how would I do to print (echo) this ??

    
asked by anonymous 18.09.2016 / 03:08

2 answers

1
The SQL would basically look like this:

select (sum(p_line_a) + sum(p_line_l)) soma, p_data from items WHERE p_data = '2016-09-20'

PHP

$bsc_user = $pdo->prepare("select (sum(p_line_a) + sum(p_line_l)) soma, p_data from tab_newprodut WHERE p_data = ?");
$bsc_user->execute(array("2016-09-20"));
$bsc_cont = $bsc_user->rowCount();
if($bsc_cont > 0)
{
    while($linha = $bsc_user->fetch(PDO::FETCH_ASSOC)) 
    {
        echo $linha['soma'].'<br>'
    }
} 
else 
{
    echo '<div class="return_box-list-u">Nenhum registro localizado.</div>';
}
    
18.09.2016 / 16:23
3

To return the sum of a column you should use SUM in the query, however if you want to include other fields as well, you must group them by GROUP BY .

Example 1

SELECT SUM(p_line_l) FROM tab_newprodut WHERE p_data = ?
  • Here I am returning only the sound of the column, so we do not have GROUP BY .

Example 2

SELECT SUM(p_line_l), p_data FROM tab_newprodut WHERE p_data = ? GROUP BY p_data
  • It was necessary to group here, since SUM adds several to provide a single answer, while p_data would be one-by-one, so using GROUP BY is basically saying that you want the sum of equal dates
18.09.2016 / 04:00