Skip to main content

Example 6. GROUP BY Raw Expression

Premium
3 min read
Dmytro Sakharuk avatar

The results of these queries will return false data because you are not grouping by the column that is the key to join the other table (order id). Obviously, a second request cannot be dispensed with, but then the algorithm is too complex. So all this makes no sense, it is better to make one request by grouping by orders.id, and by means of PHP to group by month.

Modestas avatar

Sorry, can you expand on this a bit more? It seems that you see a mistake here, but I'm not sure where (this was tested to make sure it works correctly). Would love to see if there's a mistake and improve it!

Dmytro Sakharuk avatar

Execute the query without joining the table and compare the results (order_total, total_orders).

Modestas avatar

First of all, thank you for noticing this. It does have an issue if there are more products assigned to the order_products table, and our testing had some flaws in the logic. I will update this article soon with updated solution and possibly test-cases for everyone to ues.

Sorry about this

Modestas avatar

Hi, both examples were updated and tests were added for them!

Thank you for noticing this issue.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.