pixie icon indicating copy to clipboard operation
pixie copied to clipboard

count() problem when groupBy is used

Open mtarlac opened this issue 7 years ago • 7 comments

Hello

Not sure am I missing something, but seems that count() doesn't work properly when groupBy is used

For example $query = PB::table('orders') ->leftJoin('admins', 'admins.id', '=', 'orders.admins_id') ->select('orders.*', 'admins.first_name', 'admins.last_name'); $no = $query->count();

works but

$query = PB::table('orders') ->leftJoin('admins', 'admins.id', '=', 'orders.admins_id') ->select('orders.*', 'admins.first_name', 'admins.last_name')->groupBy('orders.id'); $no = $query->count();

doesn't work and returns 1 as count.

mtarlac avatar Feb 08 '18 12:02 mtarlac

Have you looked what the raw output is of both? Can you please post that too?

eL-Prova avatar Feb 08 '18 12:02 eL-Prova

Hi

Raw query seems Ok

SELECT orders.*, admins.first_name, admins.last_nameFROMordersLEFT JOINadminsONadmins.id=orders.admins_id``

and with groupBy

SELECT orders.*, admins.first_name, admins.last_nameFROMordersLEFT JOINadminsONadmins.id=orders.admins_idGROUP BYorders.id``

both queries returns the same rows (in this case) but the first one is counted properly while the second one shows 1.

mtarlac avatar Feb 08 '18 12:02 mtarlac

Hi @mtarlac, can you please run this query in your database manually and see what it returns?

SELECT orders.*, admins.first_name, admins.last_name FROM orders LEFT JOIN admins ON admins.id=orders.admins_id GROUP BY orders.id

If it return 1 there then don't you think it's a data or querying issue?

usmanhalalit avatar Feb 17 '18 12:02 usmanhalalit

Sure but it returns >1 rows since there are more than one order...

mtarlac avatar Feb 17 '18 13:02 mtarlac

So when querying manually it returns correct and when querying using Pixie it returns 1?

On Sat, Feb 17, 2018, 19:38 mtbl [email protected] wrote:

Sure but it returns >1 rows since there are more than one order...

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/usmanhalalit/pixie/issues/180#issuecomment-366442043, or mute the thread https://github.com/notifications/unsubscribe-auth/AA74L-mkFnKmFnspDvJydgZ23Z3AghWgks5tVtZLgaJpZM4R-RuO .

usmanhalalit avatar Feb 17 '18 13:02 usmanhalalit

Yes..

Code: `$query = PB::table('orders') ->leftJoin('admins', 'admins.id', '=', 'orders.admins_id') ->select('orders.*', 'admins.first_name', 'admins.last_name')->groupBy('orders.id');

$no = $query->count();`

In this example with and without "->groupBy('orders.id')" should return the same result since there are no Count, SUM or other aggregate functions.

But in case you want to count (for example) items in order_items table and group them by order it doesn't work.

mtarlac avatar Feb 17 '18 13:02 mtarlac

I'll try to reproduce, thanks!

usmanhalalit avatar Feb 17 '18 14:02 usmanhalalit