I have a table like below having 3 columns with value ‘1’ or ‘0’.
S.no Product Qty1 Qty2 Qty3 1. Soap 1 0 1 2. Ball 1 1 0 3. Deodrant 0 0 0 4. Butter 1 0 1
How can I count the total number of ‘1’ in the table like the above in is having 6 nos? Also what if I want to count total rows having only ‘1’ value?
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
include 'Config.php';
$conn = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
// $sql = "SELECT SUM(Qty1 + Qty2 + Qty3) from Table"; doesn't seems to work
$result = $conn->query($sql);
if ($result=mysqli_query($conn,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
echo $rowcount;
// Free result set
mysqli_free_result($result);
}
else
echo "0";
}
else
echo "failed";
$conn->close();
?>
3
Answers
Hope its helpful…
You can do it like this:
See the demo.
Results:
Using conditional aggregation sum(case when…