MySQL basics --- SQL functions

1. Sum function
Sum (field name)
Select sum (summed field name) [alias] from table name
For example: sum of products:
SELECT SUM (productPrice) total product price FROM productinfo

2. Find the maximum and minimum value
Max (field name)
Min (field name)
Select max \ min (field name for the most value) [alias] from table name
You can compare the date type

3. Calculate the age
Curdate ()-calculate the current date
Datediff (large date, small date)-calculate the total number of days between the small date and the large date
Year (date)-take out the year

For example: Calculate how old a person whose birth date is November 1, 2001 is
Select ceil (datediff (curdate (), '2001-11-01') / 365)

4. Average
Avg (Average field name)
Select Avg (Average field name) [Alias] from table name

5. Calculate a few lines of data
Count (field name / *)

topic:

  1. Calculate the total income of all guests

  2. Find out what is the highest income and what is the lowest income

  3. Find the average height of guests

  4. Calculate the age of everyone

  5. Calculate the age of the youngest guest
    Expand: Show the age and name of the youngest guest:
    SELECT * FROM guestinfo WHERE guestBirthday = (SELECT MAX (guestBirthday) from guestinfo)

  6. Calculate how many guests there are

6. Single-column grouping
Select the field name to be grouped, function ( / field name) from table name group by field name to be grouped
7. Multi-column grouping
Select field name to group 1, field name to group 2, function (
/ Field name) from table name group by field name to be grouped 1, field name to be grouped 2
8. Conditional grouping query
Select field name to be grouped, function (* / field name) from table name where condition group by to be grouped Field name.

Title: 1. Query the number of people in each area grouped by area

2. Query the highest salary in each region
3. Query the number of people of each gender in each region by region and gender
4. Query the maximum salary and minimum salary in each region
5. Query based on Regional grouping, regions with wages between 5000 and 8000, and number of people

Published 32 original articles · Likes 96 · Visits 1583

Guess you like

Origin blog.csdn.net/qq_44534541/article/details/105501035