MySQL SUM

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jamie Wright

    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


  • Pedro

    #2
    Re: MySQL SUM

    Jamie Wright wrote:[color=blue]
    >SELECT SUM(`length`)[/color]
    FROM table[color=blue]
    >WHERE 1[/color]

    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:

    Comment

    • Jamie Wright

      #3
      Re: MySQL SUM

      > >SELECT SUM(`length`)[color=blue]
      > FROM table[color=green]
      > >WHERE 1[/color][/color]

      like I said - it was going to be something silly!

      Thanks for that, much appreciated.


      Comment

      • Joshua Ghiloni

        #4
        Re: MySQL SUM

        Jamie Wright wrote:[color=blue][color=green][color=darkred]
        >>>SELECT SUM(`length`)[/color]
        >>
        >> FROM table
        >>[color=darkred]
        >>>WHERE 1[/color][/color]
        >
        >
        > like I said - it was going to be something silly!
        >
        > Thanks for that, much appreciated.
        >
        >[/color]

        Use ' instead of ` ... I don't think MySQL likes backticks.

        Comment

        • Pedro

          #5
          Re: MySQL SUM

          Joshua Ghiloni wrote:[color=blue]
          >Use ' instead of ` ... I don't think MySQL likes backticks.[/color]

          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:

          Comment

          • jack

            #6
            Re: MySQL SUM

            Pedro wrote:[color=blue]
            > Joshua Ghiloni wrote:[color=green]
            >> Use ' instead of ` ... I don't think MySQL likes backticks.[/color]
            >
            > 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.[/color]

            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 :)

            --
            --- --- --- --- --- --- ---
            jack@croatiabiz .com


            Comment

            • Pedro

              #7
              Re: MySQL SUM

              jack wrote:[color=blue]
              >Pedro wrote:[color=green]
              >> 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.[/color]
              >
              >Only way? I must have missed something...[/color]

              Did you notice the column name?
              [color=blue]
              >Why use backticks at all??
              >
              >Just put the name of a column, as it is... without quotes or backticks or
              >anything :)[/color]

              Try it:

              create table xxx (
              id int UNSIGNED NOT NULL AUTO_INCREMENT,
              `from` char(2) NOT NULL,
              PRIMARY KEY(id)
              )

              insert into xxx values (NULL, 'TX'), (NULL, 'WA'), (NULL, 'OH')

              select * from xxx where from = 'TX'
              -- error on the previous line;

              select * from xxx where `from` = 'TX'


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

              Comment

              Working...