Mysql query with column created by select in other tables

Asked

Viewed 70 times

0

Good morning guys.

Assuming a business rule where I have restaurants, and each user can follow several restaurants, I create a table, table and tabela_restaurante_usuario_seguindo. Hence, when a user starts to follow a restaurant, a record in the tabela_restaurante_usuario_seguindo is created, guarding the id_restaurante and id_usuario. So far so good.

My question is the following: assuming that my user is logged in to my application, when I list the restaurants, I want to show all the restaurants, but that in the query a column is created saying whether or not the logged-in user follows that restaurant. It is possible and feasible to achieve this result through a Mysql query?

1 answer

1


I believe you can use something like:

SELECT R.*, RU.IDUsuario AS ExisteUsuario FROM tabela_restaurante R
LEFT JOIN tabela_restaurante_usuario_seguindo RU ON RU.IDRestaurante = R.IDRestaurante
WHERE RU.IDUsuario = <ID DO USUÁRIO QUE VOCÊ DESEJA PROCURAR>

In this scenario you can validate. If the content of Existeusuario is different from NULL then the user follows that restaurant.

  • Thanks brother, I got the consult =)

Browser other questions tagged

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