Search in two tables MySQL


Is it possible to give a "select" by merging two tables in MySQL? for example: I have a table named favorites that saves id and strong id . the idea is to go through the favorites table merged with the posts table, (where title, content and post etc are saved), and select all those posts according to the user_id of the favorites table?

asked by anonymous 10.10.2016 / 17:40

1 answer


Yes, it does. This is called Join . There are several possible join types, but I'm not going to focus on that because we already have this question that deals very well with the subject.

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

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)
10.10.2016 / 18:18