Totals in a sql-table

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

    Totals in a sql-table

    Hi newsgroup,

    I have a mysql table which stores points of users from a competition. I can
    display the table with php, you see which user has points and how much, the
    one with the highest points stands on one (off course).

    But, I want to display the total amount of points of all users together form
    the table standm in the column pts.

    I really don't know how to get the total value of all values together. My
    first idea is to do something with count, but this only counts the rows.

    Who can help me out.



  • Alvaro G Vicario

    #2
    Re: Totals in a sql-table

    *** RotterdamStuden ts wrote/escribió (Fri, 7 May 2004 10:04:16 +0200):[color=blue]
    > I really don't know how to get the total value of all values together. My
    > first idea is to do something with count, but this only counts the rows.[/color]

    SELECT SUM(field) FROM table

    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --

    Comment

    • RotterdamStudents

      #3
      Re: Totals in a sql-table


      "Alvaro G Vicario" <alvaro_QUITAR_ REMOVE@telecomp uteronline.com> schreef in
      bericht news:6hhawi71x2 uk.u0nu5hc17bve $.dlg@40tude.ne t...[color=blue]
      > *** RotterdamStuden ts wrote/escribió (Fri, 7 May 2004 10:04:16 +0200):[color=green]
      > > I really don't know how to get the total value of all values together.[/color][/color]
      My[color=blue][color=green]
      > > first idea is to do something with count, but this only counts the rows.[/color]
      >
      > SELECT SUM(field) FROM table
      >[/color]


      Thanks, but this doesn't work. The code gives:
      "There are Resource id #38 points scored this competition."

      The code I use is:
      <?php
      $db = mysql_connect(" localhost","use r","pass");
      mysql_select_db ("dbase",$db );

      $sql = mysql_query('SE LECT SUM( points ) FROM Standings');

      echo "There are $sql points scored this competition";

      ?>

      The amount of points scored should give 131

      What do I do wrong??

      RotterdamStuden ts.


      Comment

      • Andy Hassall

        #4
        Re: Totals in a sql-table

        On Sun, 9 May 2004 14:25:22 +0200, "RotterdamStude nts"
        <NOSPAMnewsNOSP AM@MAPSONcistro n.nederland> wrote:
        [color=blue][color=green]
        >> SELECT SUM(field) FROM table[/color]
        >
        >Thanks, but this doesn't work. The code gives:
        >"There are Resource id #38 points scored this competition."
        >
        >The code I use is:
        ><?php
        > $db = mysql_connect(" localhost","use r","pass");
        > mysql_select_db ("dbase",$db );
        >
        > $sql = mysql_query('SE LECT SUM( points ) FROM Standings');
        >
        >echo "There are $sql points scored this competition";
        >
        >?>
        >
        >The amount of points scored should give 131
        >
        >What do I do wrong??[/color]

        Read http://uk2.php.net/mysql_query
        And the examples on http://uk2.php.net/manual/en/ref.mysql.php

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        • RotterdamStudents

          #5
          Re: Totals in a sql-table

          > >Thanks, but this doesn't work. The code gives:[color=blue][color=green]
          > >"There are Resource id #38 points scored this competition."
          > >
          > >The code I use is:
          > ><?php
          > > $db = mysql_connect(" localhost","use r","pass");
          > > mysql_select_db ("dbase",$db );
          > >
          > > $sql = mysql_query('SE LECT SUM( points ) FROM Standings');
          > >
          > >echo "There are $sql points scored this competition";
          > >
          > >?>
          > >
          > >The amount of points scored should give 131
          > >
          > >What do I do wrong??[/color]
          >
          > Read http://uk2.php.net/mysql_query
          > And the examples on http://uk2.php.net/manual/en/ref.mysql.php[/color]


          Both pages aren't available and the dutch equivalent of the site doesn't
          give the solution, even aftes 100 times of reading.


          Comment

          • Andy Hassall

            #6
            Re: Totals in a sql-table

            On Sun, 9 May 2004 15:13:44 +0200, "RotterdamStude nts"
            <NOSPAMnewsNOSP AM@MAPSONcistro n.nederland> wrote:
            [color=blue][color=green][color=darkred]
            >> >Thanks, but this doesn't work. The code gives:
            >> >"There are Resource id #38 points scored this competition."
            >> >
            >> >The code I use is:
            >> ><?php
            >> > $db = mysql_connect(" localhost","use r","pass");
            >> > mysql_select_db ("dbase",$db );
            >> >
            >> > $sql = mysql_query('SE LECT SUM( points ) FROM Standings');
            >> >
            >> >echo "There are $sql points scored this competition";
            >> >
            >> >?>
            >> >
            >> >The amount of points scored should give 131
            >> >
            >> >What do I do wrong??[/color]
            >>
            >> Read http://uk2.php.net/mysql_query
            >> And the examples on http://uk2.php.net/manual/en/ref.mysql.php[/color]
            >
            >Both pages aren't available and the dutch equivalent of the site doesn't
            >give the solution, even aftes 100 times of reading.[/color]

            They were up when I sent the message, but looks like they're down at the
            moment. But I had a look at the Dutch version and it has the same examples.

            Look at each one to see what they do with the return value of mysql_query(),
            and why expected that it would print 'Resource id #38' if you tried printing it
            out directly. mysql_query() doesn't return rows, it returns a resource
            identifier that you can use with the mysql_fetch* functions to get any rows
            that may have been returned.

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

            Comment

            • RotterdamStudents

              #7
              Re: Totals in a sql-table

              > >> >The code I use is:[color=blue][color=green][color=darkred]
              > >> ><?php
              > >> > $db = mysql_connect(" localhost","use r","pass");
              > >> > mysql_select_db ("dbase",$db );
              > >> >
              > >> > $sql = mysql_query('SE LECT SUM( points ) FROM Standings');
              > >> >
              > >> >echo "There are $sql points scored this competition";
              > >> >
              > >> >?>
              > >> >
              > >> >The amount of points scored should give 131
              > >> >
              > >> >What do I do wrong??
              > >>
              > >> Read http://uk2.php.net/mysql_query
              > >> And the examples on http://uk2.php.net/manual/en/ref.mysql.php[/color]
              > >
              > >Both pages aren't available and the dutch equivalent of the site doesn't
              > >give the solution, even aftes 100 times of reading.[/color]
              >
              > They were up when I sent the message, but looks like they're down at the
              > moment. But I had a look at the Dutch version and it has the same[/color]
              examples.[color=blue]
              >
              > Look at each one to see what they do with the return value of[/color]
              mysql_query(),[color=blue]
              > and why expected that it would print 'Resource id #38' if you tried[/color]
              printing it[color=blue]
              > out directly. mysql_query() doesn't return rows, it returns a resource
              > identifier that you can use with the mysql_fetch* functions to get any[/color]
              rows[color=blue]
              > that may have been returned.
              >
              > --
              > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
              > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]

              Maybe I miss something but I don't see the fault I make. Why should I use
              mysql_fetch() as command??


              Comment

              • Jan Pieter Kunst

                #8
                Re: Totals in a sql-table

                In article <c7lfpk$vl$1@ne ws.cistron.nl>,
                "RotterdamStude nts" <NOSPAMnewsNOSP AM@MAPSONcistro n.nederland> wrote:
                [color=blue]
                > Maybe I miss something but I don't see the fault I make. Why should I use
                > mysql_fetch() as command??[/color]

                Because that is the way to actually get data from an SQL query.

                There are basically three steps to get at the actual data from the
                database:

                $link_to_databa se = mysql_connect($ host, $user, $pass);
                $resource = mysql_query($qu ery, $link_to_databa se);
                $array_with_dat a = mysql_fetch_arr ay($resource);

                JPK

                --
                Sorry, <devnull@cauce. org> is een "spam trap".
                E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                Comment

                • Matthias Esken

                  #9
                  Re: Totals in a sql-table

                  RotterdamStuden ts schrieb:
                  [color=blue]
                  > Maybe I miss something but I don't see the fault I make. Why should I use
                  > mysql_fetch() as command??[/color]

                  Because that's the way to retrieve the data from a query. mysql_query()
                  just executes the query but doesn't give you the data of a SELECT.

                  Have a look at example 1 at http://www.php.net/manual/nl/ref.mysql.php.

                  Regards,
                  Matthias

                  Comment

                  • RootShell

                    #10
                    Re: Totals in a sql-table


                    "RotterdamStude nts" <NOSPAMnewsNOSP AM@MAPSONcistro n.nederland> wrote in
                    message news:c7l805$ofn $1@news.cistron .nl...[color=blue]
                    >
                    > "Alvaro G Vicario" <alvaro_QUITAR_ REMOVE@telecomp uteronline.com> schreef[/color]
                    in[color=blue]
                    > bericht news:6hhawi71x2 uk.u0nu5hc17bve $.dlg@40tude.ne t...[color=green]
                    > > *** RotterdamStuden ts wrote/escribió (Fri, 7 May 2004 10:04:16 +0200):[color=darkred]
                    > > > I really don't know how to get the total value of all values together.[/color][/color]
                    > My[color=green][color=darkred]
                    > > > first idea is to do something with count, but this only counts the[/color][/color][/color]
                    rows.[color=blue][color=green]
                    > >
                    > > SELECT SUM(field) FROM table
                    > >[/color]
                    >
                    >
                    > Thanks, but this doesn't work. The code gives:
                    > "There are Resource id #38 points scored this competition."
                    >
                    > The code I use is:
                    > <?php
                    > $db = mysql_connect(" localhost","use r","pass");
                    > mysql_select_db ("dbase",$db );
                    >
                    > $sql = mysql_query('SE LECT SUM( points ) FROM Standings');
                    >
                    > echo "There are $sql points scored this competition";
                    >
                    > ?>
                    >
                    > The amount of points scored should give 131
                    >
                    > What do I do wrong??
                    >
                    > RotterdamStuden ts.
                    >
                    >[/color]

                    $sql = MYSQL_QUERY ( ' SELECT SUM ( field ) FROM table ) ;

                    printf ("There are %s points scored this competition", MYSQL_RESULT (
                    $sql , 0 ));


                    Try it... it should work... i guess

                    Happy Coding
                    RootShell



                    Comment

                    • Subhash

                      #11
                      Re: Totals in a sql-table

                      "RotterdamStude nts" <newsNoSpAm@mAp SoNwilleboerm.c istron.nederlan d> wrote in message news:<c7ffte$fq j$1@news.cistro n.nl>...[color=blue]
                      > Hi newsgroup,
                      >
                      > I have a mysql table which stores points of users from a competition. I can
                      > display the table with php, you see which user has points and how much, the
                      > one with the highest points stands on one (off course).
                      >
                      > But, I want to display the total amount of points of all users together form
                      > the table standm in the column pts.
                      >
                      > I really don't know how to get the total value of all values together. My
                      > first idea is to do something with count, but this only counts the rows.
                      >
                      > Who can help me out.[/color]


                      One way to do it:
                      <?php
                      $sql = "SELECT SUM(points) AS TOTAL FROM Users";
                      $result = mysql_query($sq l);
                      $row = mysql_fetch_obj ect($result);
                      echo $row->TOTAL;
                      ?>
                      -S

                      Comment

                      Working...