Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
SQL sum function to store as single variable
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jasonix  
View profile  
 More options Apr 20 2012, 2:27 am
From: jasonix <jayhina...@gmail.com>
Date: Thu, 19 Apr 2012 23:27:23 -0700 (PDT)
Local: Fri, Apr 20 2012 2:27 am
Subject: SQL sum function to store as single variable
hi, i'm newby in using cakePHP.

i have 2 tables: customerbills and customerpurchases.

what i want to do is to make a sum query in customerpurchases and
update it in customerbills.
customerpurchases fields:
-id
-customerbill_id
-product
-amount

customerbills fields:
-id
-customer_id
-amountbill

Here's what i did in my customerpurchasesController:

$billid = 1;

$total = $this->Customerpurchase->query("SELECT
SUM(customerpurchases.amount) AS total From customerpurchases Group by
customerbill_id = $billid");

now i try this for updating:
$this->Customerpurchase->query("UPDATE bills SET amountbill = $total
Where id = $billid");

but i it doesn't work. I tried to display the variable $total in
session flash to check what's inside the variable and it displays
"arrays" ..

pls. help me. Thanks..


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lowpass  
View profile  
 More options Apr 21 2012, 5:24 pm
From: lowpass <zijn.digi...@gmail.com>
Date: Sat, 21 Apr 2012 17:24:00 -0400
Subject: Re: SQL sum function to store as single variable
Because flash expects a string.

debug($total);
$this->log($total);

If you're going to use query() for this I think you'd be better off
calling stored procedures. Or, put another way, I think you'd be
better off using stored procedures.

That is, if your model's logic doesn't also include some need to loop
over the Customerpurchase data and do other stuff. If you do, then use
a regular find() and sum your total in afterFind() so you have
something like this:

array(
        'Customerpurchase' => array(
                0 => array( ... ),
                1 => array( ... ),
                ...
        ),
        'total' => $the_total
)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
kdubya  
View profile  
 More options Apr 22 2012, 2:24 pm
From: kdubya <kenwin...@winanstech.com>
Date: Sun, 22 Apr 2012 11:24:56 -0700 (PDT)
Local: Sun, Apr 22 2012 2:24 pm
Subject: Re: SQL sum function to store as single variable

Your SQL syntax in incorrect.

Try:
SELECT SUM(customerpurchases.amount) AS total From customerpurchases
WHERE customerbill_id = $billid

This can be written as a find():
$total = $this->CustomerPurchases->find('first',
array('conditions'=>array('customerbill_id'=>$billid),
'fields'=>array('sum(customerpurchases.amount) as total'));

If you then do a debug($total)  you will get something like:

Array
(
    [0] => Array
        (
            [$total] => <some_number>
        )

)

Using find() instead query() gives all kinds of benefits like increased
security and the ability to use the Containable behavior properly.

Try to follow the conventions.

HTH,
Ken


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jasonix  
View profile  
 More options Apr 23 2012, 12:09 am
From: jasonix <jayhina...@gmail.com>
Date: Sun, 22 Apr 2012 21:09:09 -0700 (PDT)
Local: Mon, Apr 23 2012 12:09 am
Subject: Re: SQL sum function to store as single variable
hi all. thanks for replying! =)

what i want to know is that how can I extract the data from the array
and store it to a variable. in ken's reply, below:

On Apr 23, 2:24 am, kdubya <kenwin...@winanstech.com> wrote:

> Your SQL syntax in incorrect.

> Array
> (
>     [0] => Array
>         (
>             [$total] => <some_number>
>         )

> )

Let's assume that the array [$total] is 300.00.

Now, how can i extract or get the [$total] value in the array and
store it as an integer/float variable? Like $extractedTotal = 300.00.

Thanks so much. =)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thiago Belem  
View profile  
 More options Apr 23 2012, 8:23 am
From: Thiago Belem <cont...@thiagobelem.net>
Date: Mon, 23 Apr 2012 09:23:12 -0300
Local: Mon, Apr 23 2012 8:23 am
Subject: Re: SQL sum function to store as single variable

Maybe:

$total = $var[0]['$total'] ??

--
***Thiago Belem*
Desenvolvedor
Rio de Janeiro - RJ - Brasil

*Assando Sites* - Curso online de *CakePHP*
assando-sites.com.br <http://goo.gl/b1EEd>

thiagobelem.net
cont...@thiagobelem.net

*Skype / gTalk **»* thiago.belem.web
*LinkedIn* *»* br.linkedin.com/in/thiagobelem/pt


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
0x20h  
View profile  
 More options Apr 23 2012, 2:48 pm
From: 0x20h <k...@informatik.uni-marburg.de>
Date: Mon, 23 Apr 2012 20:48:42 +0200
Local: Mon, Apr 23 2012 2:48 pm
Subject: Re: SQL sum function to store as single variable
Am 22.04.2012 20:24, schrieb kdubya:

> [...]
> This can be written as a find():
> $total = $this->CustomerPurchases->find('first',
> array('conditions'=>array('customerbill_id'=>$billid),
> 'fields'=>array('sum(customerpurchases.amount) as total'));

I think something like

$total = $this->CustomerPurchases->field('SUM(amount)',
array('customerbill_id' => $billid));

should do the trick


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jasonix  
View profile  
 More options Apr 25 2012, 3:33 am
From: jasonix <jayhina...@gmail.com>
Date: Wed, 25 Apr 2012 00:33:53 -0700 (PDT)
Local: Wed, Apr 25 2012 3:33 am
Subject: Re: SQL sum function to store as single variable

On Apr 24, 2:48 am, 0x20h <k...@informatik.uni-marburg.de> wrote:

> Am 22.04.2012 20:24, schrieb kdubya:

> > [...]
> > This can be written as a find():
> > $total = $this->CustomerPurchases->find('first',
> > array('conditions'=>array('customerbill_id'=>$billid),
> > 'fields'=>array('sum(customerpurchases.amount) as total'));

> I think something like

> $total = $this->CustomerPurchases->field('SUM(amount)',
> array('customerbill_id' => $billid));

> should do the trick

yes, but it still returns an array... what i want is to return a float
value..

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeremy Burns | Class Outfit  
View profile  
 More options Apr 25 2012, 3:48 am
From: Jeremy Burns | Class Outfit <jeremybu...@classoutfit.com>
Date: Wed, 25 Apr 2012 08:48:26 +0100
Local: Wed, Apr 25 2012 3:48 am
Subject: Re: SQL sum function to store as single variable

Cake finds return arrays. Simply extract the value you want from the array.

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 25 Apr 2012, at 08:33:53, jasonix wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
LITTO CHACKO  
View profile  
 More options Apr 25 2012, 3:49 am
From: LITTO CHACKO <li...@axtecindia.com>
Date: Wed, 25 Apr 2012 13:19:29 +0530
Local: Wed, Apr 25 2012 3:49 am
Subject: Re: SQL sum function to store as single variable

i think this will work
*$totalamt=$total[0]['CustomerPurchases'][''SUM(amount)'];*
*return $totalamt;*

--
Litto Chacko
Axtec Softwares

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »