Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answers from @jjclarkson and @davethegr8 are close, but you can't put aggregate functions in the WHERE clause. The WHERE clause is evaluated for each row. </p> <p>You need to evaluate the <code>MAX()</code> expression for each group, so you need to use a <code>HAVING</code> clause.</p> <p>Try this:</p> <pre><code>SELECT UserID FROM ArrivalTimes GROUP BY UserID HAVING MAX(ArrivalTime) &lt;= '09:00:00'; </code></pre> <hr> <p>@MBCook comments that <code>HAVING</code> can be slow. You're right, it might not be the absolute quickest way to produce the desired result. But the <code>HAVING</code> solution is the most <em>clear</em>. There are situations where performance has lower priority than clarity and maintainability.</p> <p>I looked at the EXPLAIN output (on MySQL 5.1.30) for the <code>HAVING</code> solution: no indexes were used, and the extra notes said "<code>Using temporary; Using filesort</code>," which usually means performance will be poor. </p> <p>Consider the following query: </p> <pre><code>SELECT DISTINCT a1.UserID FROM ArrivalTimes a1 LEFT OUTER JOIN ArrivalTimes a2 ON (a1.UserID = a2.UserID AND a2.ArrivalTime &gt; '09:00:00') WHERE a2.UserID IS NULL; </code></pre> <p>This generates an optimization plan that uses an index on <code>UserID</code> and says:</p> <ul> <li>a1: "<code>Using index; Using temporary</code>" </li> <li>a2: "<code>Using where; Distinct</code>"</li> </ul> <p>Finally, the following query generates an optimization plan that appears to use indexes most effectively, and no temp tables or filesort. </p> <pre><code>SELECT DISTINCT a1.UserID FROM ArrivalTimes a1 WHERE NOT EXISTS (SELECT * FROM ArrivalTimes a2 WHERE a1.UserID = a2.UserID AND a2.ArrivalTime &gt; '09:00:00'); </code></pre> <ul> <li>a1: "<code>Using where; Using index</code>" </li> <li>a2: "<code>Using where</code>"</li> </ul> <p>This appears most likely to have the best performance. Admittedly, I only have four rows in my test table, so this isn't a representative test.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload