SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How do you count different values from same column

    Hi,

    I've a column called 'status' and three different values stored in it, including HIGH, MIDDLE and LOW. How do I count these different values and use them as strings like this one:
    Code:
    if ( $row['HIGH'] >= 10 && $row['request'] == 3 ) {
         // the process continued
    } elseif ( BLAH BLAH BLAH ) {
         // continues BLAH BLAH BLAH
    }
    I've tried this method and it doesn't work: http://www.randomsnippets.com/2008/1...mysql-queries/

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    i did not understand your code and what it's supposed to be doing

    the way to count the statuses using mysql is as follows:
    Code:
    SELECT status
         , COUNT(*) AS rows
      FROM daTable
    GROUP
        BY status
    this produces a result set that looks like this:
    Code:
    status rows
    HIGH   42
    LOW     9
    MIDDLE 37
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that's what the result I wanted.

    The table takes values from submit form and I want to process the user's request after viewing a report.

    But how do I use that 42 rows. Here's my table stricture:
    Code:
    id      text_field     status      user_id     request
    I should really change the request to action.
    The main point is how many HIGH, LOW, MIDDLE statuses the user requested which needs to take an action? I set 1 = 'not done yet', 2 = 'rejected', 3 = 'done'. The query request all fields but I only want to count status column so I cloud review the user's request history.

    Hope I make it clear.
    Thanks,

    UPDATE: It doesn't work in the while loop.

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,338
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by ketting00 View Post
    The main point is how many HIGH, LOW, MIDDLE statuses the user requested which needs to take an action? I set 1 = 'not done yet', 2 = 'rejected', 3 = 'done'.
    possibly you want to add a WHERE clause ...
    Code:
    SELECT status
         , COUNT(*) AS rows
      FROM daTable
     WHERE request = 1 
    GROUP
        BY status
    Quote Originally Posted by ketting00 View Post
    UPDATE: It doesn't work in the while loop.
    that would be a php problem, and this is the mysql forum

    sorry, i don't do php
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •