How does MySQL count the number of different value bars in a field?

Source: Internet
Author: User
Keywords Php mysql

Statistics of different values in time span, number of occurrences at that time
, here I want to find out the number of different values in the periods field for each month within 1 years.
For example in the visible 201 May-April, periods 2 appeared 3 times, 3 appeared 1 times, the most critical is periods you do not know how many possible values, perhaps this month 1, perhaps not.

Reply content:

Statistics of different values in time span, number of occurrences at that time
, here I want to find out the number of different values in the periods field for each month within 1 years.
For example in the visible 201 May-April, periods 2 appeared 3 times, 3 appeared 1 times, the most critical is periods you do not know how many possible values, perhaps this month 1, perhaps not.

Wrote a demo, online preview

First, the time format I use is date, 年-月-日 this form

Number of statistics,count再group by 列名

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

With the Count function

You can use two group by conditions.

select count(id), periods, from_unixtime(time, '%Y-%m')from productwhere  你的条件group by periods,from_unixtime(time, '%Y-%m')

Well, that's probably the way it looks.

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

  • Related Article

    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.