Professional Web Applications Themes

MySQL SUM - PHP Development

Hey there, I've been searching for a way to add up a number of numbers from a MySQL database and have come up with the SUM() statement, but I'm not really too sure how to use it. The manual doesn't give me much in the way of assistance... SELECT SUM(`length`) WHERE 1 doesn't work at all. Any ideas? What am I doing wrong, I'm fairly sure it is going to be something very silly. Thanks Jamie...

  1. #1

    Default MySQL SUM

    Hey there,

    I've been searching for a way to add up a number of numbers from a MySQL
    database and have come up with the SUM() statement, but I'm not really too
    sure how to use it. The manual doesn't give me much in the way of
    assistance...

    SELECT SUM(`length`) WHERE 1

    doesn't work at all. Any ideas? What am I doing wrong, I'm fairly sure it is
    going to be something very silly.

    Thanks

    Jamie


    Jamie Wright Guest

  2. #2

    Default Re: MySQL SUM

    Jamie Wright wrote:
    >SELECT SUM(`length`)
    FROM table
    >WHERE 1
    Suppose you have this table (named "distance")

    id | length
    ---+--------
    1 | 17,82
    2 | 190.02
    3 | 3.99
    4 | 77.76

    The result of

    "select sum(length) from distance"

    is 289.59

    and "select sum(length) from distance where id<3"

    returns 207.84



    HTH


    --
    "Yes, I'm positive."
    "Are you sure?"
    "Help, somebody has stolen one of my electrons!"
    Two atoms are talking:
    Pedro Guest

  3. #3

    Default Re: MySQL SUM

    > >SELECT SUM(`length`)
    > FROM table
    > >WHERE 1
    like I said - it was going to be something silly!

    Thanks for that, much appreciated.


    Jamie Wright Guest

  4. #4

    Default Re: MySQL SUM

    Jamie Wright wrote:
    >>>SELECT SUM(`length`)
    >>
    >> FROM table
    >>
    >>>WHERE 1
    >
    >
    > like I said - it was going to be something silly!
    >
    > Thanks for that, much appreciated.
    >
    >
    Use ' instead of ` ... I don't think MySQL likes backticks.

    Joshua Ghiloni Guest

  5. #5

    Default Re: MySQL SUM

    Joshua Ghiloni wrote:
    >Use ' instead of ` ... I don't think MySQL likes backticks.
    Backticks in MySQL are used to delimit column names. You can have a
    column named, for instance, "from" and the only way to access it is
    with the backticks.

    The single quotes are used to delimit text, as well as the double
    quotes


    select id from table where `from` = 'TX'

    or

    select id from table where `from` = "TX"



    --
    "Yes, I'm positive."
    "Are you sure?"
    "Help, somebody has stolen one of my electrons!"
    Two atoms are talking:
    Pedro Guest

  6. #6

    Default Re: MySQL SUM

    Pedro wrote:
    > Joshua Ghiloni wrote:
    >> Use ' instead of ` ... I don't think MySQL likes backticks.
    >
    > Backticks in MySQL are used to delimit column names. You can have a
    > column named, for instance, "from" and the only way to access it is
    > with the backticks.
    Only way? I must have missed something...

    Why use backticks at all??

    Just put the name of a column, as it is... without quotes or backticks or
    anything :)

    --
    --- --- --- --- --- --- ---
    [email]jackcroatiabiz.com[/email]


    jack Guest

  7. Moderated Post

    Default Re: MySQL SUM

    Removed by Administrator
    Pedro Guest
    Moderated Post

Similar Threads

  1. Replies: 2
    Last Post: February 14th, 10:54 AM
  2. Replies: 1
    Last Post: January 23rd, 12:06 PM
  3. Replies: 0
    Last Post: January 29th, 02:03 PM
  4. Replies: 1
    Last Post: January 27th, 02:49 AM
  5. Replies: 0
    Last Post: January 24th, 10:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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