Search code examples
mysqlcountrecords

How to exclude records from mysql query count?


enter image description here

How can i count all facebook profiles from facebook column?

I used this

    $query = "SELECT COUNT(facebook) FROM members";  
    $result = mysql_query($query) or die(mysql_error()); 
    foreach(mysql_fetch_array($result) as $fbcount);

And it give result 5.

How can i make it to count just 3?


Solution

  • Your query is simply doing a COUNT on facebook column without using any conditions. And therefore the query will return as many records as you have in the table.

    Try this:

    SELECT COUNT(1)
    FROM `members`
    WHERE `facebook` != '' 
    AND `facebook` IS NOT NULL;