Professional Web Applications Themes

mysql SUM function - MySQL

Hello everyone. I have a (faily simple) question regarding mysql SUM function Here's my situation: I got 2 tables in mysql database. First one contains users and their informations and second one contains steps (yes, steps.users are able to enter amount of walked steps each day and each row equals one day). Obviously steps in Steps table are identified with ID (UserID) to establish connection to users table. And my question is: How do I select select all users from tblwalkers table and SUM of all steps for each user (that is selected from tblSteps table)? I'm trying to output ...

  1. #1

    Default mysql SUM function

    Hello everyone.

    I have a (faily simple) question regarding mysql SUM function

    Here's my situation:
    I got 2 tables in mysql database. First one contains users and their
    informations and second one contains steps (yes, steps.users are able
    to enter amount of walked steps each day and each row equals one day).
    Obviously steps in Steps table are identified with ID (UserID) to
    establish connection to users table.

    And my question is:

    How do I select select all users from tblwalkers table and SUM of all
    steps for each user (that is selected from tblSteps table)?
    I'm trying to output this list on a page using php:

    mysql_query("SELECT tblwalkers.ID, SUM(tblsteps.Steps) AS steps FROM
    tblwalkers, tblsteps WHERE tblwalkers.ID = tblsteps.userID");
    $sql_result = mysql_fetch_array($sql);

    do {

    $total_steps = $sql_result['steps'];
    $user_id = $sql_result['ID'];

    echo "member:".$user_id;
    echo "total steps:".$total_steps;
    echo "<br />";

    } while ($sql_result = mysql_fetch_array($sql));



    --
    SELECT tblwalkers.ID, SUM(tblsteps.Steps) AS steps FROM tblwalkers,
    tblsteps WHERE tblwalkers.ID = tblsteps.userID
    is not working

    nor is

    SELECT tblwalkers.ID, tblsteps.UserID, SUM(tblsteps.Steps) AS steps
    FROM tblwalkers Inner Join tblsteps ON tblwalkers.ID = tblsteps.userID


    Can anybody hook me up with a solution to this problem?
    Any help is appreciated.

    redpixel Guest

  2. #2

    Default Re: mysql SUM function

    Oh, heh, I just figured it out.

    Thanx anyway

    redpixel Guest

  3. #3

    Default Re: mysql SUM function

    "redpixel" <com> wrote in message
    news:googlegroups.com... 

    It's always nice to share the solution when answering your own question so
    that others might learn.


    Bosconian Guest

Similar Threads

  1. Replies: 1
    Last Post: January 23rd, 12:06 PM
  2. printf-like function in mysql
    By Bart Friederichs in forum MySQL
    Replies: 4
    Last Post: January 9th, 12:52 PM
  3. mysql load_file() function
    By Ben-Nes Yonatan in forum PostgreSQL / PGSQL
    Replies: 12
    Last Post: February 8th, 01:09 PM
  4. Need hep with mysql function
    By Zlut Arch in forum PHP Development
    Replies: 1
    Last Post: July 11th, 01:45 AM

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