Sum-the problem of using sum in thinkphp

Source: Internet
Author: User
The data in the table is the user's consumption points record, there is an increase, there is a decrease in consumption, I want to count a user's existing points, sum how to achieve it? No acoustic SQL.

Reply content:

The data in the table is the user's consumption points record, there is an increase, there is a decrease in consumption, I want to count a user's existing points, sum how to achieve it? No acoustic SQL.

Must there be a field for the record to be increased or decreased? Otherwise, how do you know whether to increase or decrease the points?
If the Type field 0 represents a decrease, 1 represents an increase, then

M (' Points ')->where (' type=1 ')->sum ();

If it is two fields Add_score and Reduce_score, then subtract two sum, if it is one, there must be a negative, direct sum.

If there is a field name in the table to determine whether it is increased or decreased, then use the Where to belong to the user, and is the increment of sum (), and then use a where to belong to the user and is a reduced sum (), then subtract it.
If there is no field to determine whether to increase or decrease, but instead of using the number of its own points consumption plus positive and negative storage, then the direct where belongs to the user's records and then sum can be

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