How can i count more records which have different values in same field

Discussion in 'Databases' started by Ronaldo30, May 11, 2008.

  1. #1
    I have the table with name: table witn next recording:

    +------+-------+
    | id | test |
    +------+-------+
    | 1 | 5 |
    +------+-------+
    | 2 | 6 |
    +------+-------+
    | 3 | 6 |
    +------+-------+
    | 4 | 7 |
    +------+-------+
    | 5 | 8 |
    +------+-------+
    | 6 | 9 |
    +------+-------+
    | 7 | 6 |
    +------+-------+
    | 8 | 10 |
    +------+-------+
    | 9 | 7 |
    +------+-------+

    How can i do to count how manny records i have in the field with the name: TEST with values: 5, 6 and 7;

    Teoreticaly is:

    SELECT COUNT(*) AS nr FROM table WHERE test = 5 AND test = 6 AND test = 7;

    but it doesn't work like this.

    How can i do that?

    Thanks!!
     
    Ronaldo30, May 11, 2008 IP
  2. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Use OR in place of AND

    SELECT 
    	test,
    	COUNT(*) AS nr 
    FROM 
    	table 
    WHERE 
    	(test = 5 OR test = 6 OR test = 7)
    GROUP BY test;
    Code (markup):
     
    Kuldeep1952, May 11, 2008 IP
  3. apmsolutions

    apmsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Better yet:

    
    
    SELECT 
    	test,
    	COUNT(*) AS nr 
    FROM 
    	table 
    WHERE 
    	test in (5,6,7)
    GROUP BY test;
    Code (markup):
     
    apmsolutions, May 11, 2008 IP
  4. Ronaldo30

    Ronaldo30 Peon

    Messages:
    36
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Working ....

    Thank you very much!
     
    Ronaldo30, May 11, 2008 IP