Reputation: 2477
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
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
Reputation: 3544
SELECT COUNT(*) FROM `table` WHERE `d1`='a' OR `d2`='a' OR `d3`='a';
Upvotes: 0