doni49 Posted January 2, 2016 Share Posted January 2, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/300096-reading-old-phpfreaks-threads-what-does-this-code-do-from-barands-reply/ Share on other sites More sharing options...
requinix Posted January 2, 2016 Share Posted January 2, 2016 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 @cumwhich would be like $cum = 0; for (; ; $cum = $cum + $profit) { Quote Link to comment https://forums.phpfreaks.com/topic/300096-reading-old-phpfreaks-threads-what-does-this-code-do-from-barands-reply/#findComment-1528968 Share on other sites More sharing options...
benanamen Posted January 2, 2016 Share Posted January 2, 2016 Just one thing that wasn't mentioned, it is called a user-defined variable in case you or someone else wants to look it up. Quote Link to comment https://forums.phpfreaks.com/topic/300096-reading-old-phpfreaks-threads-what-does-this-code-do-from-barands-reply/#findComment-1529000 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.