How does mysql count different values of a field?

Source: Internet
Author: User
Count different values in a time span. The number of occurrences of the period. Here, I want to query the number of times that the periods field of each month has different values in one year. For example, in the-4 month, periods appears 3 times for 2 and 3 once for 3. The most important thing is periods. You don't know how many of them exist...

Count the number of occurrences of different values in the time span
Here, I want to query the number of times each month's periods field has different values in one year.
For example, in the-4 month, periods appears 3 times for 2 and 3 once for 3. The most important thing is that periods does not know how many possible values there are, maybe there is 1 in this month, maybe not.

Reply content:

Count the number of occurrences of different values in the time span
Here, I want to query the number of times each month's periods field has different values in one year.
For example, in the-4 month, periods appears 3 times for 2 and 3 once for 3. The most important thing is that periods does not know how many possible values there are, maybe there is 1 in this month, maybe not.

I wrote a DEMO to preview it online.

The Time Format I used is date,Year-month-dayThis form

Count,Count and then group by column name

SELECT `periods`,DATE_FORMAT(time,'%Y-%m') as month,COUNT(*) AS `times` FROM `product` GROUP BY `periods`,`month` ORDER BY `month` DESC

DOC:
Date_format: http://www.w3school.com.cn/ SQL /func_date_format.asp
Group by: http://www.w3school.com.cn/ SQL/SQL _groupby.asp

Use the count function

Use two group by conditions.

Select count (id), periods, from_unixtime (time, '% Y-% m') from productwhere your condition group by periods, from_unixtime (time, '% Y-% m ')

Er, the general idea is like this.

Select periods, sum (periods) as count from product where month> = 'time' and mouth <= "time" group by periods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.