按月份对生日结果进行分组
我有一个用户列表及其生日。我需要打印每个月的生日列表并按天分组。例如:
24 日星期一 (2):Herpina Derpina、John Doe
27 日星期二 (1):Nicolas Cage
28 日星期五 (1):Michael Jackson
我怎样才能通过一个 SQL 调用来实现这一目标?如果我这样对它们进行分组,我将丢失记录:
SELECT COUNT(*) FROM user GROUP BY MONTH(birthday)
有什么想法吗? (我使用的是mysql+php)
I have a list of users and their birthdays. I need to print the list of birthdays for each month and grouped by day. For example:
Monday 24th (2): Herpina Derpina, John Doe
Tuesday 27th (1): Nicolas Cage
Friday 28th (1): Michael Jackson
How could I achieve that with one single SQL call? If I group them this way, I will lose records:
SELECT COUNT(*) FROM user GROUP BY MONTH(birthday)
Any ideas? (I'm using mysql + php)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法在同一个查询中获得作为聚合函数的计数和详细记录。如果名称是必需的,那么您将必须在 PHP 中手动构建计数,然后以所需的格式显示记录。
You can't get the count which is an aggregate function and the detailed records in the same query. If the names are essential then you will have to manually construct the counts in PHP and then display the records under the required format.