Mysql从其他表中选择nickNames?

时间:2018-03-10 01:59:42

标签: php mysql

我有3个桌面用于聊天应用

  • chatRooms(id)

  • chatMessages(id,roomId,userId,message)

  • userData(userId,nickName)

如何将所有chatRooms与chatMessages一起加载到nickNames?

我试过了:

$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();

但它似乎对我不起作用:C

我想在聊天室名称中显示聊天室中的所有用户

所以,如果3人(Fred,Joe,Bane)在小组中,那么我不知何故需要一个阵列。对于每个数组元素(chatRoom)

1 个答案:

答案 0 :(得分:0)

你的第一次LEFT JOIN很可能会返回多行。

根据您的问题,您的“chatMessages”表中还包含userId。您也应该按此过滤。

为此,您需要:

  1. 在chatMessages之前加入userData
  2. 在chatMessages上的LEFT JOIN中包含userData中的userId

    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char const *argv[]) {
      ofstream output("data4.dat", ios::out | ios::binary);
      string buffer = "A program that translates a high level languageto a machine language\n is called a compiler. A compiler is thus a somewhat peculiar sort\n of program and its output is yet another program. To avoid confusion,\n the input program is usually called the source program or\n source code, and the translated version produced by the compiler is called";
      output << buffer;
      output.close();
    
      ifstream is;
      ifstream input("data4.dat", ios::in | ios::binary);
      string in_buffer;
    
    
    
      if (! is.eof()) {
        for ( int i = 0; i < sizeof(input); i++) {
          input >> buffer[i];
        }
      }
      input.close();
      std::cout << buffer[2] << '\n';
      return 0;
    }