SQL拆分逗号从一个表中分隔列表,并根据此列表打印第二个表中的内容

时间:2018-05-15 07:35:37

标签: php mysql explode

我有两个表,roomsutilities。 在名为rooms的表中,我有一个名为utilities的coloumn。此coloumn存储逗号分隔的数字,引用表utilities中的ID。

如何分割(爆炸?)utilities coloumn中的值,并将它们与utilities表匹配,打印具有匹配ID的值作为spitted值?

示例:

表:房间
| --------------- | ---------------- | ---------------- - |
| ID | 名称 | 实用程序 |
| --------------- | ---------------- | ---------------- - |
| 1 |会议室1 | 1,3,4 |
| --------------- | ---------------- | ---------------- - |

表:公用事业
| --------------- | ---------------- |
| ID | 设备 |
| --------------- | ---------------- |
| 1 |音箱|
| --------------- | ---------------- |
| 2 |电视|
| --------------- | ---------------- |
| 3 |智能电视|
| --------------- | ---------------- |
| 4 |网络摄像头|
| --------------- | ---------------- |

我想打印这样的东西:
Room1: Speakers, Smart TV, Web camera

这是我到目前为止所拥有的,confroomreport.php:

<?php
 require_once("db.php");
 $util = $conn->getDeviceList();
 $arr = $conn->getConfRoomList();
?>


<table border='1'>
    <tr>
        <th>Room</th>
        <th>Utilities</th>
    </tr>
    <?php for( $i=0; $i < count($arr); $i++) {
    print_r  ("<tr>
     <td>".$arr[$i]['name']."</td>
     <td>".$arr[$i]['utilities']."</td>
     </tr>"); } ?>
</table>

并在db.php

public function getDeviceList()
    {
          $arr = array();
          $statement = $this->conn->prepare("SELECT id, device from utilities order by device ASC");
          $statement->bind_result($id, $device);
          $statement->execute();
          while ($statement->fetch()) {
            $arr[] = [ "id" => $id, "device" => $device];
          }
          $statement->close();

          return $arr;
    }

public function getConfRoomList()
  {
      $arr = array();
      $statement = $this->conn->prepare("SELECT id, name, utilities from rooms order by name ASC");
      $statement->bind_result($id, $name, $utilities);
      $statement->execute();
      while ($statement->fetch()) {
      $arr[] = [ "id" => $id, "name" => $name, "utilities" => $utilities];
      }
      $statement->close();

      return $arr;
  }

根据公认的解决方案编辑更新的代码:

我把它添加到我的db.php:

  public function joinDevRoom()
  {
     $arr = array();
     $statement = $this->conn->prepare("SELECT r.name,GROUP_CONCAT(u.device) FROM room r LEFT JOIN utilities u ON FIND_IN_SET(u.id,r.utilities)>0 GROUP BY r.id");
     $statement->bind_result($id, $utilities);
     $statement->execute();
     while ($statement->fetch()) {
       $arr[] = [ "id" => $id, "utilities" => $utilities];
     }
     $statement->close();

     return $arr;
  }

这是我更新的confroomreport.php:

<?php
 require_once("db.php");
 $arr = $conn->getConfRoomList();
 $join = $conn->joinDevRoom();
?>


<table border='1'>
    <tr>
        <th>Room</th>
        <th>Utilities</th>
    </tr>
    <?php for( $i=0; $i < count($arr); $i++) {
     print_r  ("<tr>
     <td>".$arr[$i]['name']."</td>"); }
     for( $u=0; $u < count($join); $u++) {
     print_r ("<td>".$join[$u]['utilities']."</td>
     </tr>"); } ?>
    </table>

1 个答案:

答案 0 :(得分:2)

试试这个

SELECT r.Name,GROUP_CONCAT(u.Device) FROM rooms r
LEFT JOIN utilities u ON FIND_IN_SET(u.id,r.Utilities)>0
GROUP BY r.ID