Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count() problem when groupBy is used #180

Open
mtarlac opened this issue Feb 8, 2018 · 7 comments
Open

count() problem when groupBy is used #180

mtarlac opened this issue Feb 8, 2018 · 7 comments
Assignees

Comments

@mtarlac
Copy link

mtarlac commented Feb 8, 2018

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 mtarlac changed the title count() problem when groupBy is user count() problem when groupBy is used Feb 8, 2018
@eL-Prova
Copy link

eL-Prova commented Feb 8, 2018

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

@mtarlac
Copy link
Author

mtarlac commented Feb 8, 2018

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.

@usmanhalalit
Copy link
Owner

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?

@mtarlac
Copy link
Author

mtarlac commented Feb 17, 2018

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

@usmanhalalit
Copy link
Owner

usmanhalalit commented Feb 17, 2018 via email

@mtarlac
Copy link
Author

mtarlac commented Feb 17, 2018

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.

@usmanhalalit
Copy link
Owner

I'll try to reproduce, thanks!

@usmanhalalit usmanhalalit self-assigned this Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants