ThinkPHP Statistics Query

Source: Internet
Author: User
We often use statistical data in applications, such as the number of current users (or the number of users meeting certain conditions), the maximum points of all users, and the average score of students, thinkPHP provides a series of built-in statistics queries for these statistics operations

We often use statistical data in applications, such as the number of current users (or the number of users meeting certain conditions), the maximum points of all users, and the average score of students. ThinkPHP
Provides a series of built-in methods for these statistical operations:

Count (): Number of statistical data rows
Max (): calculates the maximum data of a field.
Min (): calculates the minimum data of a field.
Avg (): calculates the average data of a field.
Sum (): calculates the sum of the data of a field.

The preceding statistical query methods are independent and support consistent operations.
Count ()

Count ()
The method is used to calculate the number of rows.

Example:




  1. Public function read (){
  2. $ Dao = M ('user ');
  3. // Obtain the number of users:
  4. $ UserCount = $ Dao-> count ();
  5. // Add conditions:
  6. $ UserCount2 = $ Dao-> where ('uid> 10')-> count ();

  7. $ This-> assign ('usercount', $ userCount );
  8. $ This-> display ();
  9. }

In the preceding example, the SQL statements actually executed by the two query statements are:




  1. Select count (*) AS tp_count FROM user LIMIT 1

  2. Select count (*) AS tp_count FROM user WHERE uid> 10 LIMIT 1

You can directly output the statistical data in the template:

Total users {$ userCount}
Person.

Max ()

The max () method is used to calculate the maximum data of a field.

Example of calculating the maximum user points:




  1. $ MaxScore = $ Dao-> max ('score ');

The actual executed SQL statement is:




  1. Select max (score) AS tp_max FROM user LIMIT 1

Min ()

Min ()
Calculates the minimum data of a field.

Example of getting the minimum credits of a user whose credits are greater than 0:




  1. $ MinScore = $ Dao-> where ('score> 0')-> min ('score ');

The actual executed SQL statement is:




  1. Actually executed SQL: SELECT MIN (score) AS tp_min FROM user WHERE score> 0 LIMIT 1

Avg ()

Avg ()
Calculate the average data of a field.

Example of getting average credits of a user:




  1. $ AvgScore = $ Dao-> avg ('score ');

The actual executed SQL statement is:




  1. Select avg (score) AS tp_avg FROM user LIMIT 1

Sum ()

Sum ()
Calculates the sum of the data of a field.

Calculate the sum of the points of the top 10 users in the points ranking:




  1. $ SumScore = $ Dao-> order ('score desc')-> limit ('10')-> avg ('score ');

The actual executed SQL statement is:




  1. Select sum (score) AS tp_sum FROM user order by score desc limit 1

All statistical queries, such as select ()
Same method. Consistent operations are supported, and different query conditions are added according to the actual situation.

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.