count, where question!

Discussion in 'MySQL' started by okgaz, Apr 9, 2008.

  1. #1
    Hi!

    Say I have mysql a table that has widgets in one column and price in another..

    Is it possible to count the total number of widgets and the number of widgets that cost more than 10 in one query?

    ie. count the same thing but with a different WHERE clause?
     
    okgaz, Apr 9, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Something like this. You may need to adjust the syntax but this should work.

    
    SELECT COUNT(widgets) AS count, (SELECT COUNT(widgets) FROM widgets_table WHERE widgets_cost > '10') AS count_10 FROM widgets_table;
    
    Code (markup):
     
    jestep, Apr 9, 2008 IP
  3. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Oh hey, you're the same guy that solved my last problem, thanks! :)

    I'd +rep you again but it says I have to spread it around first :\
     
    okgaz, Apr 9, 2008 IP
  4. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Ok, I'm trying to use your code now + I think I'm doing something wrong. The query I've ended up with is quite a mouthful + is returning an error:

    
    SELECT COUNT(DISTINCT mid) AS pos (SELECT COUNT (DISTINCT mid) AS tot FROM games_scores WHERE gid = 37 AND (datescored > 1207669542) ) FROM games_scores WHERE gid = 37 AND (datescored > 1207669542)  AND (score > 671 OR (score = 671 AND timespent < 26)) 
    
    Code (markup):
    Any ideas?
     
    okgaz, Apr 9, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    Try it like this. This may be easier to debug if it doesn't work.

    
    SELECT 
    (SELECT COUNT (DISTINCT mid) FROM games_scores WHERE gid = 37 AND (datescored > 1207669542) AND (score > 671 OR (score = 671 AND timespent < 26))) AS pos, 
    (SELECT COUNT (DISTINCT mid) FROM games_scores WHERE gid = 37 AND (datescored > 1207669542)) AS scores
    
    Code (markup):
     
    jestep, Apr 9, 2008 IP
  6. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #6
    The error message is now:

    
    mySQL query error: SELECT 
    (SELECT COUNT (DISTINCT mid) FROM games_scores WHERE gid = 37 AND (datescored > 1207672393)  AND (score > 671 OR (score = 671 AND timespent < 26))) AS pos, 
    (SELECT COUNT (DISTINCT mid) FROM games_scores WHERE gid = 37 AND (datescored > 1207672393) ) AS scores
    
    mySQL error: You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT COUNT (DISTINCT mid) FROM games_scores WHERE gid = 37 AN
    mySQL error code: 
    
    Code (markup):
     
    okgaz, Apr 9, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    Try putting ' around the numbers in the where clause.

    gid = '37'
    datescored > '1207672393'
    score > '671'
    etc
     
    jestep, Apr 9, 2008 IP
  8. okgaz

    okgaz Well-Known Member

    Messages:
    450
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    128
    #8
    Nope still no joy. I think it must be because I'm using MySQL4? Looks like I'll have to use seperate queries, I'm sure the syntax is right now but it still aint working :S

     
    okgaz, Apr 9, 2008 IP