Unknown column in 'having clause' Unknown column in 'having clause' database database

Unknown column in 'having clause'


As written in the documentation

The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL supports an extension to this behavior, and permits HAVING to refer to columns in the SELECT list and columns in outer subqueries as well.

You have to specify return_date and rental_date in the select clause.

There are two options:

SELECT DISTINCT  customer.first_name,  rental.return_date,  rental.rental_dateFROM  rental,  customerWHERE  rental.customer_id = customer.customer_idGROUP BY  rental.rental_idHAVING  (    rental.return_date - rental.rental_date  ) =(  ...

or

SELECT DISTINCT  customer.first_name,  (rental.return_date - rental.rental_date) as rental_durationFROM  rental,  customerWHERE  rental.customer_id = customer.customer_idGROUP BY  rental.rental_idHAVING  rental_duration =(  ...

Both should work just fine.