Jump to content

Consulta a 3 tabelas


davidconcha

Recommended Posts

davidconcha

Boas comunidade.

Tenho a seguinte consulta que me retorna, dependendo do $id de sessão as mensagens que foram enviadas para este utilizador logado com id=6, por exemplo.

$sql="SELECT
chat.id, chat.reciever, chat.sender, chat.msg, chat.time,



IF(chat.sender = $id, utilizadores_reciever.username,  utilizadores_sender.username) as username

FROM
chat

INNER JOIN user as utilizadores_sender ON (chat.sender = utilizadores_sender.id)
INNER JOIN user as utilizadores_reciever ON (chat.reciever = utilizadores_reciever.id)

WHERE
chat.sender = $id OR chat.reciever = $id
GROUP BY
IF(chat.sender = $id, utilizadores_reciever.id,  utilizadores_sender.id)


";

Na tabela do user, eu guardo um campo photo_p_id, onde faço referência ao o id da foto contida na tabela photos.

Agora...como poderei receber as fotos dos utilizadores que enviaram mensagens ao user id=6, por exemplo?

Algo deste género:

FROM user AS u
INNER JOIN photos AS p ON u.photo_p_id=p.id

Edited by davidconcha
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.