Mysql query with column created by select in other tables

0

Good morning guys.

Assuming a business rule where I have restaurants, and each user can follow several restaurants, I create a table_restaurant , table_user and table_restaurante_usuario_continued . So when a user starts to follow a restaurant, a record in the restaurant_table_folder_folder is created, saving the id_restaurant and user_id. So good.

My question is this: assuming my user is logged in to my app, when I list restaurants, I want all restaurants to be shown, but in the query a column is created that says whether or not the logged in user restaurant. Is it possible and feasible to achieve this result through a Mysql query?

    
asked by anonymous 29.10.2015 / 13:04

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 may be worth it. If the content of ExistUsuario is different from NULL then the user follows that restaurant.

    
29.10.2015 / 13:24