Results 1 to 3 of 3

Thread: Database Query with "SUM"

  1. #1
    New user
    Join Date
    09-20-15.
    Posts
    19

    Frage Database Query with "SUM"

    Hello Community,

    I have a DB query with sum
    PHP Code:
    $this->Database->prepare("SELECT SUM(money) FROM `tl_finance_transactions` WHERE headcategory = ? AND `subcategory` = ? AND YEAR(FROM_UNIXTIME(`transactionsdate`)) = ? AND MONTH(FROM_UNIXTIME(`transactionsdate`)) = ? AND `client` = ?")->execute($headcategory,$subcategory,$year,$month,$UserId)->next(); 
    When i dump the results i get :
    PHP Code:
    Contao\Database\Mysqli\Result Object ( [resResult:protected] => mysqli_result Object ( [current_field] => [field_count] => [lengths] => Array ( [0] => ) [num_rows] => [type] => ) [strQuery:protected] => SELECT SUM(moneyFROM `tl_finance_transactionsWHERE headcategory AND `subcategory` = 's,13' AND YEAR(FROM_UNIXTIME(`transactionsdate`)) = '2015' AND MONTH(FROM_UNIXTIME(`transactionsdate`)) = AND `client` = '1' [intIndex:Contao\Database\Result:private] => [blnDone:Contao\Database\Result:private] => [blnModified:Contao\Database\Result:private] => [arrCache:protected] => Array ( [SUM(money)] => 180.00 ) ) 
    but when i whant the data "[SUM(money)]" i do
    PHP Code:
    return $test->SUM(money
    only i get a error "Fatal error: Uncaught exception Error with message Call to undefined method Contao\Database\Mysqli\Result::SUM()"

    How can i get the data "[SUM(money)]"

    Thanks in advance

  2. #2
    New user cliffen's Avatar
    Join Date
    10-18-13.
    Location
    Lüneburg
    Posts
    22

    Default

    Hi, i think you hav to use an alias.

    Something like this:

    Code:
    SELECT SUM(money) as mysum FROM `tl_
    And the use it in the database result:

    PHP Code:
    return $test->mysum 
    Extensions :: Github :: No support via PM.

  3. #3
    New user
    Join Date
    09-20-15.
    Posts
    19

    Default

    Thanks,

    This solves my problem.
    totally forgot about that function in SQL.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •