title: MySQL: WHERE-clause in query with COUNT and GROUP BY?
body: <p>For each category used for my adverts i want to show the total count of adverts.</p>
  
  <p>My current query looks like this:</p>
  
  <pre><code>SELECT
      categories_adverts.category,
      COUNT(adverts.id) AS numberOfAdverts
  FROM adverts
  LEFT JOIN categories_adverts
  ON adverts.id_categories_adverts=categories_adverts.id
  GROUP BY category ASC
  </code></pre>
  
  <p>I am struggling with including a WHERE clause which specificies that only those adverts are shown whose duration is still valid. I tried to include the following clause between "FROM adverts" and "LEFT JOIN ...":</p>
  
  <pre><code>WHERE TIME_TO_SEC(timediff(now(),adverts.stamp_created))&lt;=adverts.duration
  </code></pre>
  
  <p>So the whole query would like like this, but this does NOT work:</p>
  
  <pre><code>SELECT
      categories_adverts.category,
      COUNT(adverts.id) AS numberOfAdverts
  FROM adverts
  WHERE TIME_TO_SEC(timediff(now(),adverts.stamp_created))&lt;=adverts.duration
  LEFT JOIN categories_adverts
  ON adverts.id_categories_adverts=categories_adverts.id
  GROUP BY category ASC
  </code></pre>
  
  <p>What is wrong about the query?</p>