CakePHP中的多个计数 [英] Multiple Counts in CakePHP
问题描述
我有一个使用CakePHP的PHP应用程序。我需要提供一个表,显示每月每一天的特定事件的数量。每个事件都记录在DB中,名称和数据。为此创建表的最佳方法是什么,我真的需要执行31个SQL调用来计数每一天的事件,或者取出整个月的数据,然后在我的应用程序中解析它,或者有一个
I have a PHP application using CakePHP. I need to present a table showing the count of particular events for each day of the month. Each event is record in the DB with a name and a Data. What is the best way to create a table for this, do I really need to do 31 SQL calls to count the events for each day, or take out the data for the whole month and then parse it in my app, or is there a better find command to use with cake?
推荐答案
您可以COUNT个事件, GROUP BY 发生的日期。
You could COUNT the events, and GROUP BY the date on which they occurred.
示例:
SELECT eventDate, COUNT() as 'events'
FROM events
GROUP BY eventDate
ORDER BY eventDate ASC
这可能会导致类似以下结果:
Which may render results similar to the following:
2009-08-11 | 23
2009-08-09 | 128
2009-08-06 | 15
如果将日期和时间存储为eventDate,则需要使用substr函数获取按日期分组的事件:
If you store date and time as the eventDate, you'll need to use the substr() function to get events grouped by just the date:
SELECT substr(eventDate, 1, 10) as 'date', COUNT() as 'events'
FROM events
GROUP BY substr(eventDate, 1, 10)
ORDER BY eventDate ASC
这篇关于CakePHP中的多个计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!