SQL COUNT(condition)?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rossco_2005
    Senior Member
    • Apr 2005
    • 465
    • 4.1.x

    SQL COUNT(condition)?

    Hi,

    I have two queries:
    Code:
    SELECT COUNT(*) AS comments
    FROM ng_comment
    WHERE ng_comment.collectionid = 47[FONT=Tahoma]
    [/FONT]
    Code:
    SELECT COUNT(*) AS comments2
    FROM ng_comment
    WHERE ng_comment.collectionid = 47
    AND ng_comment.dateline <= 1262487011
    Is there a way that I can combine these?
    I was thinking something like:
    Code:
    SELECT COUNT(*) AS comments, COUNT(IF ng_comment.dateline <= 1262487011) AS comments2
    FROM ng_comment
    WHERE ng_comment.collectionid = 47
    But I'm not sure what the syntax for this is in MySQL.

    Any help? Thanks.

    http://filesharingtalk.com

  • rossco_2005
    Senior Member
    • Apr 2005
    • 465
    • 4.1.x

    #2
    I figured it out for myself.
    I used:
    Code:
    SELECT COUNT(*) AS totalcomments, COUNT(CASE WHEN ng_comment.dateline <= 1262487011 THEN 1 ELSE null END) AS newercomments
    FROM ng_comment
    WHERE ng_comment.collectionid = 47

    http://filesharingtalk.com

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...