Get 3 counts from a table
You can select multiple counts from the table based on a condition using a CASE WHEN statement, and when writing SQL or something where formatting helps readability it's normally a good idea to use heredoc.

$getSQL = &lt;&lt;&lt;SQL
SELECT
    COUNT(DISTINCT guid) AS allNum,
    COUNT(DISTINCT CASE WHEN chk='Y' THEN guid END) AS openNum
FROM `{$table}`
WHERE
    1=1
    {$addSQL}
SQL;

$listData = $sql-&gt;Select($getSQL);
$arr = $listData['data'][0];

$arr['closeNum'] = $arr['allNum'] - $arr['openNum'];

echo json_encode($arr); 
exit;