I have three tables, tbl_recursos
, tbl_categorias
, and tbl_categoria_recurso
. The tbl_recursos
consists of the listing of resources posted by the user. tbl_categorias
are the categories that a resource can have. And finally, tbl_categoria_recurso
makes the relationship between resources and categories.
- tbl_recursos -
id
data
- tbl_categorias -
id
nome
- tbl_categoria_recurso -
id
id_recurso
id_categoria
What I want to do is list the names of the categories that have been associated with more resources in the last week. That is, the top 5 categories with more resources in the last week. I am trying to do the following query but it gives error in the date:
<?php
$sql = mysql_query("SELECT * FROM tbl_categorias cat WHERE
(SELECT 1 FROM tbl_categoria_recurso cat_r
JOIN tbl_recursos re
ON re.id = cat_r.id_recurso
WHERE cat_r.id_categoria = cat.id
AND (re.data >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND re.data < curdate () - INTERVAL DAYOFWEEK (curdate ()) - 1 DAY))); ? > ? >
The error appears in the following image:
Andtheimagethatprovesthatthedatefieldexistsintbl_recursos: