PHP - Return column sum at date specifies // PDO

Asked

Viewed 228 times

0

How to make that in return fetch(PDO::FETCH_ASSOC) he adds the columns p_line_a (height) and p_line_l (width) referring to a specific date.

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

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

inserir a descrição da imagem aqui

The general return, however not summed and tbm separate...

        $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 sum the two columns p_line_a, p_line_l that refer to the specified date.
2- how would I print (echo) this??

  • Like this yours query on the PDO?

  • $bsc_line = $Pdo->prepare("SELECT * FROM tab_newprodut WHERE p_data = ?");

2 answers

3

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

Example 1

SELECT SUM(p_line_l) FROM tab_newprodut WHERE p_data = ?
  • Here I’m just returning the sound of the speaker, so we don’t have GROUP BY.

Example 2

SELECT SUM(p_line_l), p_data FROM tab_newprodut WHERE p_data = ? GROUP BY p_data
  • Here it was necessary to group, because the SUM sum several to provide a single answer, while p_data would be one-by-one, then using the GROUP BY basically if you’re saying you want the sum of the same dates.
  • 1- it needs to sum the two columns in total p_line_a, p_line_l referring to the specified date. 2- how would I print (echo) that??

1


To SQL basically would be 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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.