简体   繁体   English

MYSQL-如何计算每月出现的次数

[英]MYSQL - How to count the number of occurrences per month

I have a table with a varchar column and a datetime column that I want to get some counts from: 我有一个表,其中包含一个varchar列和一个datetime列,我希望从中获得一些计数:

CREATE TABLE `appointment` (
  `appointmentId` int(10) NOT NULL AUTO_INCREMENT,
  `cid` int(10) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `location` text NOT NULL,
  `contact` text NOT NULL,
  `start` datetime NOT NULL,
  PRIMARY KEY (`appointmentId`));

Some values: 一些值:

INSERT INTO `appointment` (`cid`, `title`, `description`, `location`, `contact`, `start`) 
VALUES 
('1', 'text1', ' ', ' ', ' ', '2017-07-21 16:00'),
('2', 'text1', ' ', ' ', ' ', '2017-08-21 16:00'),
('1', 'text1', ' ', ' ', ' ', '2017-09-21 16:00'),
('1', 'text1', ' ', ' ', ' ', '2017-09-21 16:00'),
('3', 'text2', ' ', ' ', ' ', '2017-07-21 16:00'),
('1', 'text3', ' ', ' ', ' ', '2017-07-21 16:00'),
('1', 'text4', ' ', ' ', ' ', '2017-07-21 16:00'),
('5', 'text5', ' ', ' ', ' ', '2017-07-21 16:00'),
('1', 'text1', ' ', ' ', ' ', '2017-07-21 16:00'),
('1', 'text1', ' ', ' ', ' ', '2017-07-21 16:00'),
('1', 'text3', ' ', ' ', ' ', '2017-07-21 16:00');

What I want to accomplish is to get a count of the occurrences of a 'title' on a monthly basis. 我要完成的工作是每月统计一次“头衔”的发生次数。 Something like this: 像这样:

Month       | title | count 
--------------------------- 
July        | text1  | 3 
August      | text1  | 1  
September   | text1  | 2 
July        | text2  | 1 
etc...

I have gotten as far as 我已经到了

SELECT  MONTHNAME(start) AS 'MONTH',
    title AS 'TITLE',
    count(title) AS 'COUNT'
FROM appointment
GROUP BY TITLE

But, that is grouping all of text1 into a total count on the first month of occurrence. 但是,这是在出现的第一个月将所有text1分组为总数。 So, for the example above it is showing: 'July | 因此,对于上面的示例,它显示为:“ 7月| text1 | text1 | 6' 6'

I've tried several variations on that query, but I'm clearly missing something. 我已经对该查询尝试了几种变体,但显然缺少一些东西。

Can someone explain: a) why is it aggregating all of those into one month? 有人可以解释:a)为什么将所有这些汇总到一个月内? and b) how to get the query to produce the results I want? 和b)如何获取查询以产生所需的结果?

Not sure if you tried this or not but you need to group by month like: 不知道是否尝试过,但是需要按月分组,例如:

SELECT  MONTH(start) AS MONTH,
    title AS TITLE,
    count(title) AS COUNT
FROM appointment
GROUP BY title,MONTH(start);

Plus why do you need quotes after AS ...? 再加上为什么在AS ...之后需要引号?

You need to group by monthname(Start) as well so GROUP BY Title, MonthName(start) . 您还需要按monthname(Start)进行分组,因此也要按GROUP BY Title, MonthName(start)进行分组。

The reason why you're seeing July text1 6 is because all of text1 is being put into July, not just july. 您看到7月text1 6的原因是因为所有text1都放入了7月,而不仅仅是7月。 Typically any non-aggregated field int eh select should be in the group by. 通常,选择的任何非聚合字段都应在分组依据中。 mySQL extends the group by to not thow an error when a non-aggregated field isn't in the group by; 当未聚合的字段不在group by中时,mySQL扩展了group by不会发出错误; whereas oracle, db2, SQL Server Postgresql and others would all throw an error. 而oracle,db2,SQL Server Postgresql和其他将引发错误。 There is a setting which can be disabled in mySQL which would cause your SQL to error since monthname(start) wasn't in the group by. 有一个可以在mySQL中禁用的设置,由于monthname(start)不在分组依据中,这将导致SQL错误。

https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM