MySQL first knowledge (v) Statistics and calculation and time

Source: Internet
Author: User

MySQL Date and time


*/
Get the current date and time
Mysql> Select Now ();
+---------------------+
| Now () |
+---------------------+
| 2015-10-28 19:51:17 |
+---------------------+

Get Current date
Mysql> select Curdate ();
+------------+
| Curdate () |
+------------+
| 2015-10-28 |
+------------+

Get current time
Mysql> select Curtime ();
+-----------+
| Curtime () |
+-----------+
| 19:57:01 |
+-----------+

DateTime () Gets the date and time
Date () Get dates
Time ()
Year ()
Month ()
Day () days
Hour ()
Minute () min
Second () seconds

All of these are interception formats like this
Mysql> Select year (' 20150303 ');
+------------------+
| Year (' 20150303 ') |
+------------------+
| 2015 |
+------------------+
Mysql> Select Date (' 20150303 ');
+------------------+
| Date (' 20150303 ') |
+------------------+
| 2015-03-03 |
+------------------+

/**
MySQL statistics and calculations (this is more important)


*/
Count how many data
Mysql> Select COUNT (*) from Stuinfo;
Get the minimum age value
Mysql> Select min (age) from Stuinfo;
Get maximum value with Max
Mysql> select Max (age) from Stuinfo;
Sum is obtained by sum ()
Mysql> select SUM (age) from Stuinfo;
Get average
Mysql> Select AVG (age) from Stuinfo;

All of these are group by ... that serve in groups ...
Group calculation of total number of male and female students
Mysql> Select Sex,count (*) from stuinfo GROUP by sex;
+------+----------+
| sex | COUNT (*) |
+------+----------+
| Male | 3 |
| Women | 3 |
+------+----------+

Having is used in conjunction with group by, and then adding group conditions after the combination is complete
Mysql> Select Sex,count (*) from Stuinfo GROUP by sex has sex= ' male ';
+------+----------+
| sex | COUNT (*) |
+------+----------+
| Male | 3 |
+------+----------+

MySQL first knowledge (v) Statistics and calculation and time

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.