Retrieve data from another mysql table and quantity

Asked

Viewed 1,297 times

4

I’m having a lot of doubts about Mysql when I have to use INNER JOIN to retrieve data from other tables.

I have 3 tables:

MEDIA

id, user_id, title, Description

COMMENTS

id, user_id, media_id, comment

MEDIA_LIKES

id, user_id, media_id

The MEDIA table is the main one, it contains the id do post and id do usuário, then I have the comments table where you have the comments posted in certain posts and then the like also given in these posts.

My big doubt is like through a Query in Mysql I can get the table data MEDIA, the amount of comments and likes data for given media_id?

2 answers

3


your query will be like this:

SELECT
  (SELECT COUNT(*) FROM comments WHERE media_id='214') as comentarios, 
  (SELECT COUNT(*) FROM media_likes WHERE media_id='214') as likes,
  a.*
from media a where a.id='214';

This if you want to pull only 1 id now if you want to everyone just take the Where.

---OBS pos Edit--- where ta 214 vc put your $var

SS inserir a descrição da imagem aqui

  • friend, I tried like this <br><br> "SELECT Count(b.id) as Likes, Count(c.id) as comments, a.* from media a, media_likes b, comments c WHERE a.id=214;" <br><br> ?

  • it was badly I had pasted but it was not everything , already I answered the query

  • Jasar, it worked now bro, thank you so much for your help gave straight :D thank you very much, would never be able to mount a query so >( thanks even for the help

  • Dude, honestly, I got to a point where I didn’t know how to assemble the queries, my breaking point was when I started to understand how it works, the logic itself. Try to draw the query that helps. you draw the tables and one meets the other this he the basic query you do after is only for the rest of the conditions necessary to pull the info q vc need.

  • I will try to follow this logic, but still not being able to understand how it works rs, one last doubt I saw now, not being able to recover the result of the number of comments, what can be happening, my query with your help was like "SELECT Count(b.id) as Likes, Count(c.id) as comments, a.* from media a, media_likes b, comments c Where a.id='214' and a.id=b. media_id and a.id=c.media_id;"

  • seems right, has comments with media_id 214?

  • has 1, more ta appearing 3 like media_likes, ta repeating, if I put 1 more in media_likes it repeats in the call I make from Count to comment

  • Can you give a copy of this db for me to test ? then I mount the query and t step

  • below is the download file http://www.filedropper.com/teste_4 Thank you very much. Thank you :D

  • solved ta edited up there the working query

  • Tu é sinistro mano! thanks a lot, thanks a lot for your help, you saved me! thanks a lot, thanks a lot :D

  • rsss q that I need he only put here. c I did not have for help others will. as you tbm am learning. good luck there

  • tmj Leke, Rigadão and Valeu!

Show 8 more comments

1

I don’t know, but I think it would be that way:

SELECT media.id, 
       media.user_id, 
       media.title, 
       media.description, 
       count(comments.id) quantidade_comentarios, 
       count(media_likes.id) quantidade_likes
FROM media_likes
INNER JOIN comments.user_id=media_likes.user_id AND comments.media_id=media_likes.media_id
INNER JOIN media.id=comments.media_id AND media.user_id=comments.user_id
WHERE media.id=10;
GROUP BY media.id, media.user_id, media.title, media.description

where WHERE media.id=10 in place of 10 the value you want filter.

  • did not work out friend, ta giving error :( thanks for your help

  • what is the error, maybe it is some letter, inform me the error?

  • this one: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' [Mysql][ODBC 5.3(a) Driver][mysqld-5.6.30-cll-Lve]You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '.user_id AND comments.media_id = media_likes.media_id INNER JOIN media.id = Comm' at line 1

  • @Patrique your tables are as you specified in the query, because I looked at your question and reproduced the equal fields, including checked, could you check if the fields correspond? If by chance you want to edit and paste an image of the three tables this can also help solve your problem. Remember that the error is syntax (as it is written and this may be wrong in your question).

Browser other questions tagged

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