Laravel 5.3: Syntax error or access violation: 1463 Non-grouping field 'distance' is used in HAVING clause
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Fixing the Eloquent Headache: Resolving the HAVING Clause Error in Laravel 5.3 and Beyond
This is a classic scenario that trips up many developers when moving between different database systems or upgrading framework versions. If you’ve encountered the error message: "Syntax error or access violation: 1463 Non-grouping field 'distance' is used in HAVING clause," especially when dealing with complex calculated fields in an Eloquent query that uses raw SQL expressions, you know you’ve hit a roadblock between your application logic and the underlying database engine.
This post will dive deep into why this error occurs, how MySQL enforces these rules, and provide practical, robust solutions for handling complex filtering in Laravel applications.
Understanding the Core Conflict: ONLY_FULL_GROUP_BY
The root cause of this issue lies not directly in Laravel or Eloquent, but in the strictness of your underlying database configuration, specifically MySQL's SQL mode. The error message is a direct consequence of MySQL's ONLY_FULL_GROUP_BY mode being enabled by default in many modern installations.
When you use aggregate functions (like SUM(), AVG(), COUNT()) or rely on derived columns (like your calculated distance) within the HAVING clause, MySQL requires that any non-aggregated column used in the filter must also be present in the GROUP BY clause. Since your original query calculates a value (distance) but doesn't explicitly group by all other selected fields contextually for this specific operation, MySQL throws an error because it cannot guarantee the grouping integrity required by strict SQL standards.
Your raw query:
select *, SQRT( POW((x - 860.0000),2) + POW((y - 105.0000),2) ) AS distance from poi where status = 1 and id not in (1) having distance <= 6 order by distance asc
This query attempts to filter based on a calculated field (distance) without grouping the results appropriately for that specific calculation context, leading to the violation.
Solutions: How to Handle Calculated Fields Correctly
As a senior developer, we don't just want to patch errors; we want to implement robust solutions. Here are three effective ways to resolve this issue in a Laravel environment:
1. The Database Configuration Fix (The Quickest Way)
For many developers working with data science or complex geospatial calculations where strict grouping isn't strictly required for simple filtering, the fastest solution is often to relax the SQL mode. You can modify your MySQL server configuration file (my.cnf or my.ini) and change the setting:
sql_mode=NO_ENGINE_SUBSTITUTION
Or, if you are comfortable with the risk, you could temporarily set ONLY_FULL_GROUP_BY to OFF. While this bypasses the strictness check, it is generally recommended to fix the query logic rather than disabling critical safety features.
2. Reframing the Query with Subqueries (The Eloquent Way)
A more idiomatic and robust Laravel approach is to move complex calculations or filtering into a subquery where grouping rules are clearer. Instead of calculating everything in the main SELECT and then filtering in HAVING, calculate the necessary data first, and then filter based on those results.
If you are using Eloquent, you can often achieve this by performing the heavy lifting in a separate query or by ensuring your initial selections adhere to standard grouping rules before applying the final filters. This aligns with the principles of clean data access that Laravel promotes when interacting with the database.
3. Using Raw Expressions for Filtering (The Pragmatic Way)
If the goal is purely filtering based on a calculated value, sometimes bypassing the HAVING clause entirely and moving the condition to the WHERE clause—if possible—or using a more direct approach can solve the problem. For complex geospatial calculations, ensuring that the base data meets the criteria before final ordering is key.
In essence, when dealing with raw SQL expressions in Laravel, treat the database as the ultimate source of truth regarding its strictness. Always test your queries across different environments (like MySQL Workbench vs. your application server) to catch these subtle configuration differences early on. For deep dives into optimizing your data access patterns, exploring advanced features within the Laravel ecosystem is highly recommended.
Conclusion
The error you faced is a common symptom of strict SQL mode enforcement in MySQL interacting with complex, calculated fields in HAVING clauses. By understanding the role of ONLY_FULL_GROUP_BY, you can choose between relaxing server settings or refactoring your Eloquent queries to use more standard SQL patterns. Always prioritize readable, maintainable code while ensuring your database configuration supports the integrity of your application's data logic.