Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using MySQL SELECT SUM


Go to End


4 Posts   4685 Views

Avatar
novaweb

Community Member, 116 Posts

26 March 2009 at 11:57am

Is there a SilverStripe function that has similar behavior to using the

SELECT SUM(NumVisit) FROM 'Member';
statement?

Cheers

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 2:43am

Hi!

If you can load the data into a DataObjectSet, you can use that class' Count() method.

$submissions = DataObject::get('BrowserPollSubmission');
$total = $submissions->Count();

(Example from: http://doc.silverstripe.org/doku.php?id=tutorial:3-forms#showing_the_poll_results)

Docs:
http://api.silverstripe.org/sapphire/model/DataObjectSet.html#Count

Hope that helps,
Ben

Avatar
Hamish

Community Member, 712 Posts

27 March 2009 at 8:42am

Edited: 27/03/2009 8:43am

Not quite, Count will return the number of members, not the sum of their visits.

There is an SQL query object that can do this sort of thing:

$sqlQuery = new SQLQuery(
	"SUM(NumVisit)", // Select
	"Member", // From
	"" // Where (optional)
);
$totalVisits = $sqlQuery->execute()->value();

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 March 2009 at 9:07am

Oops! I'm not sure why I thought subvert was referring to SQL Count instead of Sum. Thanks for catching that, Hamish!