Sum a column of values from a MySQL query

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Sum a column of values from a MySQL query

    I am trying to sum a query of values from a MySQL table. The code I am
    using is:

    ---BEGIN CODE #1--------------
    $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    FROM $tb_name
    WHERE invoiceid = \"$invoiceid\"
    ";

    $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result_2)) {
    $invoicepartpaid = $row['partpaid'];
    }
    ---END CODE #2----------------

    1) The code returns the sum of a partially paid invoice. The individual
    invoice is 'partpaid'. WORKS...NO PROBLEM
    2) The while() then return a list of partially paid invoices which is
    $invoicepartpaid. WORKS..NO PROBLEM
    3) I then want to add the list of partially paid invoices ($invoicepartpaid)
    together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.

    If anyone can help I would greatly appreciate it.


    --------------------------------------------------------------------------
    The content of this email message and any attachments are confidential and
    may be legally privileged, intended solely for the addressee. If you are
    not the intended recipient, be advised that any use, dissemination,
    distribution, or copying of this e-mail is strictly prohibited. If you
    receive this message in error, please notify the sender immediately by reply
    email and destroy the message and its attachments.

    Ben C. Guest

  2. Similar Questions and Discussions

    1. MySQL Query Cache Not Working: MySQL 5 / Windows XP
      Please excuse this post if you've already read it on mailing.database.mysql - i just discovered these other groups. future posts will be...
    2. php/mysql query insert values into enters the records in reverse order
      I'm loading tab delimited lines from a txt file using php and running a query for each line to enter the data into a mysql database. However the...
    3. multiple menu/list values inserted into one mysql column
      Simple problem here for the pros I'm sure. I have constructed a "rockshow" database in mysql for local music artists/bands and I made an input form...
    4. [PHP] Sum a column of values from a MySQL query
      I am trying to sum a query of values from a MySQL table. The code I am using is: ---BEGIN CODE #1-------------- $sql_2 = "SELECT...
    5. how to Add different controls(textBox,DropDownList or some ) in the same column,based upon the value in the previous column (Say second Colum which contain dropdown with some values) ?
      I am new to ASP.NET. I am facing problem with datagrid. I will explain prob now. In the datagrid the first column is name in the second...
  3. #2

    Default Re: [PHP] Sum a column of values from a MySQL query

    Quoting "Ben C." <benc@cox.net>:
    > I am trying to sum a query of values from a MySQL table. The code I am
    > using is:
    >
    > ---BEGIN CODE #1--------------
    > $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    > FROM $tb_name
    > WHERE invoiceid = \"$invoiceid\"
    > ";
    >
    > $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());
    >
    > while ($row = mysql_fetch_array($result_2)) {
    > $invoicepartpaid = $row['partpaid'];
    > }
    > ---END CODE #2----------------
    >
    > 1) The code returns the sum of a partially paid invoice. The individual
    > invoice is 'partpaid'. WORKS...NO PROBLEM
    > 2) The while() then return a list of partially paid invoices which is
    > $invoicepartpaid. WORKS..NO PROBLEM
    > 3) I then want to add the list of partially paid invoices ($invoicepartpaid)
    > together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
    Well, instead of doing $invoicepartpaid = $row['partpaid']; you could do
    $invoicepartpaid += $row['partpaid']; which will just add $row['partpaid'] to
    $invoicepartpaid, not replace it.

    However, why not just SUM all the rows in the table in the query if you just
    want a total?

    $sql_2 = "SELECT SUM(partpaidamount) as partpaid FROM $tb_name";

    --
    Adam Alkins
    [url]http://www.rasadam.com[/url]
    Adam Alkins Guest

  4. #3

    Default Re: Sum a column of values from a MySQL query

    What are you expecting back from the sql statement?

    And what are you getting?

    From what I can tell, if you are getting all your amounts from the select
    statement then you are already totalling them up inside the select
    statements by using the sum() function call.

    if you are wanting, or expecting, a row for each invoice, then you need to
    remove the where clause and the sum() alias and then loop through the result
    set and add partpaidamount to a empty variable.

    also, I just noticed that you are using double quotes in your statement. I
    have always had bad luck with using double quotes. I would switch to single
    quotes.

    how are you getting the $invoiceid value? is this code inside another a
    larger loop?

    Jim lucas

    ----- Original Message -----
    From: "Ben C." <benc@cox.net>
    To: <php-general@lists.php.net>; <curt@zirzow.dyndns.org>
    Cc: <phplist@zonedzero.net>
    Sent: Monday, August 04, 2003 10:33 PM
    Subject: Sum a column of values from a MySQL query

    > I am trying to sum a query of values from a MySQL table. The code I am
    > using is:
    >
    > ---BEGIN CODE #1--------------
    > $sql_2 = "SELECT SUM(partpaidamount) as partpaid
    > FROM $tb_name
    > WHERE invoiceid = \"$invoiceid\"
    > ";
    >
    > $result_2 = @mysql_query($sql_2,$connection) or die(mysql_error());
    >
    > while ($row = mysql_fetch_array($result_2)) {
    > $invoicepartpaid = $row['partpaid'];
    > }
    > ---END CODE #2----------------
    >
    > 1) The code returns the sum of a partially paid invoice. The individual
    > invoice is 'partpaid'. WORKS...NO PROBLEM
    > 2) The while() then return a list of partially paid invoices which is
    > $invoicepartpaid. WORKS..NO PROBLEM
    > 3) I then want to add the list of partially paid invoices
    ($invoicepartpaid)
    > together. I AM STUCK HERE ADDING THE INVOICES AMOUNTS TOGETHER.
    >
    > If anyone can help I would greatly appreciate it.
    >
    >
    > --------------------------------------------------------------------------
    > The content of this email message and any attachments are confidential and
    > may be legally privileged, intended solely for the addressee. If you are
    > not the intended recipient, be advised that any use, dissemination,
    > distribution, or copying of this e-mail is strictly prohibited. If you
    > receive this message in error, please notify the sender immediately by
    reply
    > email and destroy the message and its attachments.
    >
    >
    Jim Lucas Guest

  5. #4

    Default Re: [PHP] Re: Sum a column of values from a MySQL query

    On Tuesday 05 August 2003 23:22, Jim Lucas wrote:
    > also, I just noticed that you are using double quotes in your statement. I
    > have always had bad luck with using double quotes. I would switch to
    > single quotes.
    His sql statement assignment looks fine to me. Whether you mainly use
    single-quotes or double-quotes is irrelevant to the end result as long as
    they are used correctly. Luck has no part in determining whether your
    statement is correctly formed. I just want to point this out so the OP
    doesn't go barking up the wrong tree.

    --
    Jason Wong -> Gremlins Associates -> [url]www.gremlins.biz[/url]
    Open Source Software Systems Integrators
    * Web Design & Hosting * Internet & Intranet Applications Development *
    ------------------------------------------
    Search the list archives before you post
    [url]http://marc.theaimsgroup.com/?l=php-general[/url]
    ------------------------------------------
    /*
    I am an optimist. It does not seem too much use being anything else.
    -- Winston Churchill
    */

    Jason Wong Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139