Magento unknown column in having clause

Magento unknown column in having clause

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento unknown column in having clause

Magento: Solving the “Unknown Column in Having Clause” Error

You’ve encountered an error while trying to use the having clause in a custom collection grid for your Magento store. The error message states that the column ‘rdy_price’ is unknown, leading you to wonder why Magento isn’t joining the necessary table.

Understanding the Issue: Why Isn’t “table_url” Being Joined?

The problem lies in how Magento processes the collection and generates the SQL query. When you call $collection->getSelect()->having("rdy_price > min_sell_price AND min_sell_price > 0");, Magento doesn’t understand that ‘rdy_price’ is a calculated field derived from the joined ‘table_url’.

The error occurs because Magento’s query builder expects all columns in the having clause to be actual table columns, not calculated fields. It doesn’t automatically infer that ‘rdy_price’ is a computed value from the joined subquery.

The Missing Connection: What Happened to “table_url”?

You’re seeing this error because when you call $collection->getSelect()->group("e.entity_id");, Magento creates a new subquery for the GROUP BY operation. However, it doesn’t carry over any of the joined tables (like ‘table_url’) into this new subquery.

So even though you’ve joined ‘table_url’ earlier in your code with $collection->joinTable(), that join is only applied to the main query, not the subsequent subqueries for GROUP BY or HAVING.

The Solution: Move the Having Clause Before Group By

To resolve this issue, you need to move the having clause before the groupBy operation. This ensures that the calculated fields are available in the context of both operations.

Here’s how you can modify your code:

$collection->getSelect()->having("rdy_price > min_sell_price AND min_sell_price > 0");
$collection->getSelect()->group("e.entity_id");

Notice that I’ve placed the having clause before the groupBy. This change tells Magento to apply the filter conditions (including calculated fields) before it aggregates the data.

Best Practices for Magento Collection Queries

When working with custom collection grids in Magento, remember these best practices:

  1. Order your operations: Make sure you call the methods in a logical order. Filters should come before joins and aggregations.

  2. Understand query generation: Familiarize yourself with how Magento generates SQL queries from your collection setup. This knowledge can help prevent errors like the one you encountered.

  3. Test with small datasets first: When developing complex queries, start with a smaller product dataset to ensure your logic works before scaling up.

Conclusion: Resolving the “Unknown Column” Error

In summary, to fix the “Unknown column in having clause” error:

  1. Move the having clause call before the groupBy.
  2. Ensure all necessary joins are applied before applying filters.
  3. Follow best practices for Magento collection queries.

Remember, understanding how Magento generates SQL queries is key to writing efficient and error-free custom code. If you need further assistance or want to explore more advanced Magento development techniques, feel free to visit Laravel Company for in-depth guides and professional services.

Happy coding! Let me know if you have any other questions.

Database Admin Pricing Errors

Our network