Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help! -Adding rows and values, then sorting

    Hi,

    I'm kind of confused what i should do here. Here is my picture of database structure:


    So what i want to do is select all the rows and add the cash/points a user accumulated from individual rows, and finally pull all the rows and sort them based on the number of rows per each user, Descending, where the user with the most "entries" or rows in the table are listed first. Any idea on how i can do that?

    -prizegrand

  • #2
    New Coder
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Not 100% what you're asking, but this might help to get you started

    Code:
    mysql_query("SELECT SUM(cash), SUM(points) FROM table WHERE (Your parameters go here)";
    That will return two arrays, one is the sum of the cash one, and the other is the sum of the points accumulated, depending on what you're searching for.

    More details would help.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Can it just be so it counts the number of rows each user has and lists them by the most to the least?
    So its like this:
    User
    1
    1
    1
    2
    2
    66
    99
    22
    Where the user with ID of 1, will be listed first, and 2 second, and 66, 99, 22 listed last.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Add a GROUP BY userid to the query to keep userid => sum values. Alias the values and add an ORDER BY to which you want to order them by.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sounds good.
    How can i make it numbered in a list, like:
    Place | Username | Entries
    1 test 97
    2 dddddd 85
    3 testttt 75
    4 fdasf 65
    5 dsffsd 55
    Where i can list it by place, and have numbers accompany it? Would i use an ordered list <ol> ?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What does Entries represent?

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Entries represent the number of rows the user has in the table. Generally, 1 entry is one row

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Then its not a sum, its a count.
    Code:
    SELECT username, count(*) AS cnt FROM table GROUP BY username ORDER BY cnt DESC
    Use your iteration to add the Place, as that appears to be just an incrementing row value.


  •  

    Tags for this Thread

    Posting Permissions

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