How PHP calculates the sum of the data in a field of MySQL

Source: Internet
Author: User
Tags php write
How PHP calculates the sum of the data in a field of MySQL

Reply content:

How PHP calculates the sum of the data in a field of MySQL

    • phpUse foreach traversal to accumulate the fields you want to sum

    • mysqlcan be used SUM

SELECT sum(`field_name`) FROM `table`

Now that you can use MySQL, why not just use the sum of MySQL?

PHP Write a foreach loop compile it again

Ask the question of the Lord, MySQL's sum is more efficient, or PHP's foreach faster.

The sum of the foreach computed data
foreach ($income as $key = = $val) {

        $total+= $val['xx'];              }

The sum () function of MySQL

Yes, why not use the SUM function, the select SUM('field') where ... data is a special point with some of the functions of MySQL in the format of a bit.

If it is a one-dimensional array direct array_sum
If it is a two-dimensional array can use Array_column, array_sum to do
It feels good to do it through MySQL.

It is usually an array, the direct count ($data) can find the total number of data

  • 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.