meandme
meandme

Reputation: 2477

Counting data occurence from mysql table

I want to count the number of times for example data 'a' occurs from all the table below that from column d1,d2 and d3. For example here the number of times 'a' occurs from the table is 3.

d1| d2 | d3 |
-------------
a| ab | a |
a| ab  | ab|
ab| av | ac |
zx| cx  | zz|

I use

SELECT SUM(d1='a') + SUM(d2='a') + SUM(d3='a') FROM my_table

and

    $result = mysql_query($sql);
$row = mysql_fetch_assoc($result); 
$allsum = $result[0];
echo '<pre>';
            echo $allsum;
            echo '</pre>';

but the echo does not work

thnk for any solutions.

Upvotes: 0

Views: 349

Answers (3)

echo_Me
echo_Me

Reputation: 37233

try this

   SELECT SUM(d1='a') + SUM(d2='a') + SUM(d3='a') as allsum FROM my_table



  $result = mysql_query($sql);
  $row = mysql_fetch_assoc($result); 


        echo $row['allsum'];

Upvotes: 2

khaverim
khaverim

Reputation: 3544

SELECT COUNT(*) FROM `table` WHERE `d1`='a' OR `d2`='a' OR `d3`='a';

Upvotes: 0

eggyal
eggyal

Reputation: 125845

SELECT SUM(d1='a') + SUM(d2='a') + SUM(d3='a') FROM my_table

See it on sqlfiddle.

Upvotes: 2

Related Questions