I have 3 tables for a chat app
chatRooms (id)
chatMessages (id, roomId, userId, message)
userData (userId, nickName)
How can I load all the chatRooms with the chatMessages with the nickNames?
I tried:
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id
LEFT JOIN userData
ON chatMessages.userId = userData.userId '
);
$qry->execute();
But it doesn't seem to work for me:C
I would like to display all the users who are in the chatRoom in the chat name
So if 3 people (Fred, Joe, Bane) are in the group then I somehow would need an array with them. For every array element(chatRoom)
Your first LEFT JOIN is more than likely returning more than one row.
From your question, your "chatMessages" table also has a userId in it. You should also filter by this.
In order to do this, you would need to:
Include the userId from userData in the LEFT JOIN on chatMessages
$qry = $db->prepare('SELECT chatRooms.id, chatMessages.message, userData.nickName FROM chatRooms LEFT JOIN userData ON chatMessages.userId = userData.userId LEFT JOIN chatMessages ON chatMessages.roomId = chatRooms.id AND chatMessages.userId = userData.userId'); $qry->execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.