0

I have this database and queries:

id  product_id  status
======================
1   25          1
2   25          1
3   25          0
4   26          1
etc.

$query1 = "SELECT COUNT(id) FROM abc WHERE product_id = '25' AND status = '1'";
$query2 = "SELECT COUNT(id) FROM abc WHERE product_id = '25'";

Basically I need to count twice:

  1. Count products with id=25 which has status=1, and

  2. Count products with id=25

So, in returns I would like to have result:

There's 2 good items from 3 products

Is there a simple way to have this into single query?

[!] Sorry for bad explaination :p

Mikael
  • 21
  • 5
  • Not tested since I don't have the data, but it could look like this: `SELECT COUNT(o.id) total, (SELECT COUNT(i.id) FROM abc i WHERE i.product_id = o.product_id AND i.status = '1') AS total_active FROM abc o WHERE o.product_id = '25';` Note: You probably use an integer (or a boolean if there's only two possibilities) for the status instead a string for the status ID and product ID. – Jonathan Parent Lévesque Jun 06 '16 at 18:33
  • @JonathanParentLevesque Great, it's work... Thx a lottt =) – Mikael Jun 06 '16 at 18:44

0 Answers0