简体   繁体   English

MYSQL Innerjoin仅显示一条记录

[英]MYSQL Innerjoin only show one record

I am busy with a ticket systeem and i have the question in a table, users information in an other table and the responses in an other table. 我忙于票务系统,我在一个表中有问题,在另一个表中有用户信息,在另一个表中有响应。

What i want is to get the Question (Table 1) with the user information by user ID and all the responses (also with user information by id) 我想要的是通过用户ID和所有响应(也包括用户ID)来获得带有用户信息的问题(表1)

Now i have the following code: 现在我有以下代码:

  public function ticketSingle($id=""){
  $sql = "
  SELECT ticketSubmitted.*, users.Name, users.email, users.phone, ticketResponse.*
  FROM ticketSubmitted

  INNER JOIN users
  ON users.id = ticketSubmittedUserId

  JOIN ticketResponse
  ON ticketId = ticketSubmittedID

  WHERE ticketSubmittedID = '".$id."'
  ";

  $result = $this->run($sql, $bind);
  return $result[0];
}

Table 1 = ticketSubmitted (the question, with an userID "ticketSubmittedUserId") 表1 = ticketSubmitted(问题,用户ID为“ ticketSubmittedUserId”)
Table 2 = users (The user information) 表2 =用户(用户信息)
Table 3 = ticketResponse (The table with the reactions) 表3 = ticketResponse(带有反应的表)

But if i Print the results i get only one record of the TicketResponse and what i want is all the reactions. 但是,如果我打印结果,我只会得到一张TicketResponse记录,而我想要的就是所有反应。

Can someone help me out? 有人可以帮我吗?

This is what the function return: 这是函数返回的内容:

Array
(
    [ticketSubmittedID] => 1
    [ticketSubmittedUserId] => 1
    [ticketSubmittedDate] => 2018-02-05 16:00:00
    [ticketSubmittedTitle] => Question Title
    [ticketSubmittedMessage] => Hello World!
    [ticketSubmittedUserIp] => XXX.XXX.XXX.XXX
    [ticketSubmittedStatus] => 1
    [Name] => John Doe
    [email] => john@doe.com
    [phone] => 0612345678
    [ticketResponseId] => 1
    [ticketId] => 1
    [ticketUserId] => 2
    [ticketMessage] => Hello Reaction
    [ticketDate] => 2018-02-05 17:05
    [ticketIp] => XXX.XXX.XXX.XXX
)

You are returning only the first row return $result[0]; 您只返回第一行return $result[0]; 0 in the first index of the result. 结果的第一个索引为0。 so you should return all eg: 因此,您应该返回所有内容,例如:

return $result;

and loop over the result for manage what you need 并遍历结果以管理所需的内容

 foreach($result as $key => $value){
  echo $value['ticketResponseId'];     
 }

You can check the real return content using 您可以使用来检查真实的退货内容

 var_dump($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM