catfan / Medoo

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

Feature request: Please allow HAVING without GROUP #954

Closed kakenbok closed 3 years ago

kakenbok commented 3 years ago

Describe the bug Currently, a GROUP field is required for applying a having clause. However, according to some obscure websites like: https://dba.stackexchange.com/questions/57445/use-of-having-without-group-by-in-sql-queries/57453 this is not required and bloats the code a litte bit.

Information

Detail Code I may workaround the GROUP requirement by subclassing Medoo - but I don't like this approach, it's you great class, and I don't want to supersede or mess your work and thoughts ;-)

class MedooWithHavingWithoutGroup extends Medoo
{
    protected function whereClause($where, &$map)
    {
        if (isset($where['HAVING']) && !isset($where['GROUP'])) {
            $where['GROUP'] = 'id';
        }

        return parent::whereClause($where, $map);
    }
}

Thanks for considering :-)

catfan commented 3 years ago

It's allowed with no limitation on c5cda0f, and will be available on the next v2.0 soon.

But you have to take care query if using it without GROUP BY for the case with aggregate function executed.

kakenbok commented 3 years ago

Here is a cat for you: https://giphy.com/gifs/TikTokItalia-carino-gattino-sternuto-Q7jvImsNUcdzuGRtLd

Thank you!