1

At the moment I'm able to count all the record's in a table with the value "Waiting". This is done by using:

$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status='Waiting'";

When I want to echo the row count I just do:

echo $rows['SUM'];

How can I do it so it also count's all the record's in a table with the value "Ready"?

1
  • Do you want to count everyone who is either waiting or ready or do you need to count everyone who is waiting and then everyone who is ready? Commented Apr 8, 2016 at 14:13

4 Answers 4

3

This query will return all status count divided by status:

SELECT status, COUNT(status) AS tot FROM jobs GROUP BY status

The resulting set is something like this:

status       tot
-----------  ------
Waiting      123
Ready        80
...          56
Sign up to request clarification or add additional context in comments.

Comments

0

So you want status='Waiting' or status='Ready'? You can separate parameters with OR or AND; for example:

$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status='Waiting' OR status='Ready'";

Comments

0
SELECT count(status) AS 'count' FROM jobs GROUP BY status HAVING status='Ready'

This will count the records with the status = Ready. You use GROUP BY to get an aggregate on the status field. And because you have used GROUP BY, you use HAVING (which is the equivalent of WHERE in GROUP situations) to filter the data.

Comments

-1

By using IN operator in your query.

$query = "SELECT COUNT(*) AS SUM FROM jobs WHERE status IN ('Waiting','Ready')";

Get group based(status) counts by using GROUP BY operator

$query = "SELECT COUNT(*) AS SUM FROM jobs GROUP BY status";

Print the status based result.

//Create the `mysqli_connection` and assign to `$conn` variable.
$result = mysqli_query($conn, $query); 

if($result->num_rows>0)
{
    while($row = mysqli_fetch_assoc($result))
    {           
        echo "<br>status=" . $row['status'];
        echo "<br>SUM=" . $row['SUM'];
    }
}

2 Comments

How can I echo the count of the rows individually. For example the total jobs Waiting, and the total jobs Ready.
Check in my edited answer. It will help you to print the result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.