How to use php to query monthly sales: 1. Create a php sample file; 2. Use php to connect to the database; 3. Use the SQL statement "select year(ordertime), month(ordertime),sum( Total) from xxx group by year(ordertime),month(ordertime)" can be used to query and count monthly sales data.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
php How to check monthly sales?
SQL statement counts daily, monthly, and yearly data
1 2 3 4 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 |
|
For example, to check March 2010 Daily records
Answer
1 |
|
The format of date here is YYYY-mm-dd hh:mm:ss
1 2 3 4 5 6 7 8 9 |
|
Use SELECT statements to statistically summarize data
Aggregation functions (aggregation functions, statistical functions)
In order to effectively process the data collection obtained by the query, SQL Server provides a series of statistical functions.
These functions can implement data collection and summary:
avg ([ALL|DISTINCT] column name) Find the average of the specified numeric field
sum ([ALL|DISTINCT] column name) Find the sum of the specified numeric field
max ([ALL|DISTINCT] column name) Find Find the maximum value in the specified numeric field
min ([ALL|DISTINCT] column name) Find the minimum value in the specified numeric field
count ([ALL|DISTINCT] column name) Find the specified field in the record that satisfies the condition and is not empty Number of records
count(*) Find the total number of records that meet the conditions
Use the GROUP BY clause to classify and summarize records
Format:
GROUP BY grouping field name list[HAVING Conditional expression]
Function: Group the specified fields in sequence according to the specified conditions for statistical summary
Note:
Statements using GROUP BY can still be sorted by the ORDER BY clause
But they must be in GROUP You can use aliases after BY but you are not allowed to sort columns not specified by SELECT
The HAVING clause is to filter the query results after group statistics.
Select statements using GROUP BY can still use the WHERE clause to specify conditions
Note:
The field specified by Select must contain and only contain the grouping field specified in the GROUP BY clause (an alias can be specified for it),
Others must be composed of an aggregate function or multiple calculated columns, the columns used in statistical functions are not restricted.
Aliases of fields or calculated columns are not allowed in the GROUP BY clause, expressions can be used directly.
GROUP BY clause specifies expressions When formula is used, the field specified by select does not need to include this expression.
HAVING clause does not allow the use of aliases
HAVING clause must be used together with GROUP BY, and the set conditions must be consistent with those specified by the GROUP BY clause Recommended learning about grouping fields: "
The above is the detailed content of How to check monthly sales in php. For more information, please follow other related articles on the PHP Chinese website!