catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

[Having => [count(tbl2.id) => 0]] is converted in 'having count = 0' #916

Closed Fluit closed 3 years ago

Fluit commented 4 years ago

Describe the bug When i use ['HAVING' => [count(tbl2.id) = 0] and where tbl2 is a joined table with a table tbl1, then is this is converted in the PDO-select statement as "HAVING count = 0" in stead of "HAVING count(tbl2.id) = 0" like I'm suspecting.

Information

Detail Code

$join = '[>]tbl2';
$where = [
            'AND' => $condition,
            'ORDER' => [
                $sort => $order
            ],
            'LIMIT' => [$offset, $rows],
            'GROUP' => ['tbl1.id'],
            'HAVING' => [count(tbl2.id) => 0]
        ];

$result['rows'] = $this->Db->select('tbl1', $join, $columns, $where);

sql-string output:

SELECT tbl1.id, tbl1.col2,  count(tbl2.id) AS count
FROM tbl1 LEFT JOIN tbl2 ON tbl1.tbl2_id = tbl2.id 
GROUP BY tbl1.id 
HAVING count = 0
ORDER BY tbl1.id ASC

Expected output

SELECT tbl1.id, tbl1.col2 count(tbl2.id) AS count
FROM tbl1 LEFT JOIN tbl2 ON tbl1.tbl2_id = tbl2.id 
GROUP BY tbl1.id
HAVING count(tbl2.id) = 0 
ORDER BY tbl1.id ASC
catfan commented 3 years ago

If you want to use SQL built-in function, you need to use raw object.

"HAVING" => Medoo::raw("COUNT(tbl2.id) = 0")