Relation between 3 tables and find the result of last week mysql

Asked

Viewed 181 times

1

I have three tables, the tbl_recursos, tbl_categorias and the tbl_categoria_recurso. To tbl_recursos is the listing of resources posted by the user. A tbl_categorias are the categories a resource can have. And finally the 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 you’ve been assigned most resources in the last week. That is, the top 5 categories with more resources in the last week. I am trying to make the following query but gives error in 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 < Kurdish() - INTERVAL DAYOFWEEK(Kurdish())-1 DAY))"); ?> ?>

Error appears in the following image: inserir a descrição da imagem aqui

And the image that proves that the date field exists in tbl_resources: inserir a descrição da imagem aqui

  • Share the error.

  • @juniorb2ss added the image above. thanks

  • The error refers to the missing or unknown column required in the condition. You created the alias re of tbl_recursos, correct. Try to remove the quotes from the re.data, or change to re.data.

  • yes, the date field exists. I edited the post and entered the tbl_resources image. And I also tried it without the quotes

  • See my comment, use re.data.

  • We didn’t miss a EXISTS right after the first WHERE?

  • @juniorb2ss, still giving error in date. I do not understand why

  • @pc_oc already answered, see the answer.

Show 3 more comments

2 answers

0

Try:

 $sql = mysql_query("SELECT * FROM tbl_categorias cat WHERE 
                    (
                        SELECT 1 FROM tbl_categoria_recurso cat_r
                        JOIN tbl_recursos r
                        ON `r`.`id` = cat_r.id_recurso
                        WHERE cat_r.id_categoria = `cat`.`id`
                        AND `r`.`data` >= (curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY)
                        AND `r`.`data` < (curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY)
                    )
                ");

I haven’t quite understood what you really want to return in the subselect, because you are making a condition in the primary select capturing a record of the secondary select, you should not return the id to be used?

namely p.e:

SELECT * FROM tbl_categorias cat WHERE (SELECT `cat_r`.`id` WHERE ...))
  • When I try your query still give error anything date, I can not understand why it gives error in this field..

  • Pass me the schema through sqlfiddle.com, and I will mount the query.

  • wait just a little. thanks

  • sqlfiddle does not accept me the sql code generated by phpmyadmin. some changes need to be made?

  • No, just insert it there. There must be some syntax differences, just fix it.

  • I’m not succeeding. Don’t know any other alternative to sqldiffle? I’m sorry

  • now appears the following error: #2014 - Commands out of Sync; you can’t run this command now

  • This error and given the data range, I believe. My suggestion would be that you better treat this directly in PHP and just inform the date in the correct for the condition.

  • Yes, but how could I improve this by PHP? The question is that I have to fetch the result 6 days before the current date. thanks for the help!

  • Just look for the native php class of date manipulation. Very simple pick up last week with it.

Show 5 more comments

0


I ended up using the following solution:

<?php
    date_default_timezone_set('Europe/Lisbon');
    $data_actual = date("Y-m-d");
    $data_last_week = date('Y-m-d', strtotime($data_actual. ' - 7 days'));


    $sql1 = mysql_query("SELECT COUNT(tbl_categoria_recurso.id_categoria) as contagem, tbl_categoria_recurso.id_categoria FROM tbl_recursos INNER JOIN tbl_categoria_recurso ON tbl_recursos.id=tbl_categoria_recurso.id_recurso WHERE data > '$data_last_week' GROUP BY id_categoria order by contagem desc LIMIT 5 ");

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.