<pre><code>SELECT 
  avg(con_hits) as avg_hits 
FROM 
  content 
WHERE 
  con_type = 1 
  AND con_posttime &lt; $twelve_hrs_ago 
  AND con_refresh = 0 
ORDER BY 
  con_posttime DESC 
LIMIT 100
</code></pre>

<p>I would like it to go to the first record that was posted at least 12 hours ago (denoted by the <code>$twelve_hrs_ago</code> variable which has the suitable timestamp), and take the average of the <code>con_hits</code> column, for the next 100 records. In my example, it disregards the <code>LIMIT</code>, and takes the average of every record in the table.</p>

<p>Is there a way to bypass that?</p>

```
LIMIT
```
is applied to the resultset, after
```
AVG
```
is calculated. You can do what you want, with a subselect:
```
```
SELECT avg(con_hits) as avg_hits
FROM (
  SELECT con_hits
  FROM content
  WHERE
    con_type = 1
    AND con_posttime < $twelve_hrs_ago
    AND con_refresh = 0
  ORDER BY con_posttime DESC
  LIMIT 100
) x;
```
```
You can use the database to calculate the time offset too. Replace
```
$twelve_hrs_ago
```
above with:
```
```
date_add(now(), interval -12 hour)
```
```