Search in two Mysql tables

Asked

Viewed 131 times

7

Is it possible to "select" by merging two tables in Mysql? for example: I have a table called "favorite" that saves the id_da_postagem and the id_do_usuario. the idea would be to go through the table favorite merged with the table posts, (where are saved title, content and etc from the post itself), and select all these posts according to the id_do_usuario table favorite?

1 answer

5


There is. That’s called Join. There are all kinds of possible Join’s, but I’m not going to focus on that because we already have that question that handles the subject very well.

In your case it is very simple, see in the example. I made an Inner Join of the table Favoritos with the table Postagens whenever the countryside Id table Postagens is equal to the field Id_da_Postagem on the table Favoritos.

Select Post.Id, Post.Descricao, Post.Etc --Colunas que quero retornar/mostrar
From Favoritos Fav -- Tabela principal
Inner Join Postagens Post On Post.Id = Fav.Id_da_Postagem And Fav.Id_do_Usuario = 850; -- Condição (850 é só um Id de exemplo)
  • Thanks, that’s what I was looking for, only it seems like he’s pulling posts more than once.

  • Ahh, this really can happen. It is best to leave the user’s condition inside the Join. This will cause only the posts of the particular user to be returned. I edited the response.

  • You have not changed the order from and from Internet, right?

  • Man, sorry, it was my same error of syntax, it’s already working, thank you very much!

Browser other questions tagged

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