Jump to content

Reading old phpfreaks threads -- what does this code do (from Barand's reply)?


Recommended Posts

I'm reading through some older threads on the forum -- I find that reading other people's issues and the answers they receive is a great education in itself -- and saw some code in a reply posted by Barand that I don't understand.

 

Here's a link to the post in question http://forums.phpfreaks.com/topic/299309-cumulative-sum-in-mysql-view-calculating-running-profitloss/?do=findComment&comment=1525829

 

The code in question: 

JOIN (SELECT @cum:=0) init
@cum:=@cum+profit as cum_profit

I get that the @ symbol indicates a global variable.  But what is the := for?  And is this "init" being used similar to a for loop?

 

A single equals sign means comparison; @cum=0 would be saying "is @cum equal to 0".

Colon+equals does assignment and will set @cum to 0 in the first part and then add profit in the second part.

 

And yes, the init part is working like the init part in a for loop.

for ($cum = 0; ; $cum = $cum + $profit) {
You could do the assignment in a separate query beforehand

SELECT 0 INTO @cum
which would be like

$cum = 0;
for (; ; $cum = $cum + $profit) {
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.