Home » Php » How To Counting in Group Php MySql

How To Counting in Group Php MySql

Posted by: admin February 25, 2020 Leave a comment

Questions:

How to Counting “Active People” in Table Transaction Join to Table People,
my db :
People Table

id | Name |
===========
1  | A    |
2  | B    |
3  | C    |
4  | D    |

Transaction Table

idtrx | trxone  | trxtwo |  idpeople |
======================================
1     | 200000  |  0     |     1
2     | 5000    |  10    |     1
3     |   0     |  200   |     4
4     |   0     |  200   |     3

im sorry i’m not good at speaking english,
I mean how to join and count active people or make transactions from the 2 tables
and make a result :
Active People

[
  {
   trxone: "1", //people
   trxtwo: "3"  //people
  }
]

My code before :

<?php
require_once "conn.php";
$query = "SELECT
COUNT(DISTINCT tbl_trx.trxone) as mf
FROM tbl_people
INNER JOIN tbl_outlet ON tbl_people.id = tbl_trx.idpeople
WHERE trxone IS NOT NULL AND WHERE id = $_GET[id]";
$sql = mysqli_query ($link,$query);
$ray = array();
while ($row = mysqli_fetch_array($sql)) {
  array_push($ray, array(
    "totaltrxone" => $row['mf']
  ));
}
echo json_encode($ray);
mysqli_close($link);
?>
How to&Answers:
SELECT COUNT(DISTINCT CASE WHEN trxone THEN trxone END) trxone,
       COUNT(DISTINCT CASE WHEN trxtwo THEN trxtwo END) trxtwo
FROM tbl_people