title: Unknown column in 'having clause'
body: <p>I need to find in sakila database the longest rental period of a movie.
  I have tried this:</p>
  <pre><code>  SELECT DISTINCT
        customer.first_name
      FROM
        rental,
        customer
      WHERE
        rental.customer_id = customer.customer_id
      GROUP BY
        rental.rental_id
      HAVING
        (
          rental.return_date - rental.rental_date
        ) =(
        SELECT
          MAX(countRental)
        FROM
          (
          SELECT
            (
              rental.return_date - rental.rental_date
            ) AS countRental
          FROM
            rental,
            customer
          GROUP BY
            rental.rental_id
        ) AS t1
      )
  </code></pre>
  <p>but I am getting the error:</p>
  <pre><code># 1054 - Unknown column 'rental.return_date' in 'having clause'
  </code></pre>
  <p>Does anybody know why? I have used a column that's supposed to be the aggregated data. What am i missing?</p>