Select multiple with data from both MySQL tables

0

I am trying to perform a query in which I select several data from both tables where their relationship is NickName

at the beginning to take the messages had this code

SELECT * FROM $tabla WHERE UserEmitter IN ('$usuarioActual', 
'$usuarioReceptor') AND UserReceiver IN ('$usuarioReceptor', '$usuarioActual') 
AND seen = 1 ORDER BY created_at ASC

but now I want to also take the image of each user to show them in the view, I try to do it with

SELECT DISTINCT m.*
FROM messages m
WHERE m.UserEmitter IN ('cesgdav', 'cesgdav2') AND m.UserReceiver IN ('cesgdav2', 'cesgdav') AND seen = 1
ORDER BY created_at ASC
(SELECT picture
FROM users u
WHEREu.nickname IN ('cesgdav', 'cesgdav2'))

but when running the query it indicates an error in the second select

    
asked by Cesar Gutierrez Davalos 16.07.2018 в 17:35
source

1 answer

0

In my humble knowledge, you must perform a inner join ;

SELECT DISTINCT m.*,users.campo_nuevo
FROM messages m
INNER JOIN users ON users.campo_comun = messages.campo_comun
WHERE m.UserEmitter IN ('cesgdav', 'cesgdav2') AND m.UserReceiver IN ('cesgdav2', 'cesgdav') AND seen = 1
ORDER BY created_at ASC

I explain to you, what you are doing here is to join the 2 tables by 1 common field, in your case it would be "nickname" (?), in the section of SELECT you add the field you want from the table that you are joining.

In case you want to expand this information a bit more about link

    
answered by 16.07.2018 в 18:06