php - SQL 从一个表中拆分逗号分隔列表,并根据此列表从第二个表中打印内容
问题描述
我有两张桌子,rooms
和utilities
。在名为rooms
I 的表中,有一列也称为utilities
. 此列存储引用表中 ID 的逗号分隔数字utilities
。
如何拆分(分解?)列中的值utilities
,并将它们与utilities
表匹配,将具有匹配 ID 的值打印为吐出的值?
例子:
表:房间
|---------------|----|------------- ----|
| 身份证 | 姓名 | 实用程序 |
|---------------|----------------|---------------- -|
| 1 | 房间1 | 1,3,4 |
|---------------|----------------|---------------- -|
表:实用程序
|---------------|----------------|
| 身份证 | 设备 |
|---------------|----------------|
| 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>
解决方案
试试这个
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
推荐阅读
- python - Gimp 无法从 OSX 中的 shell 脚本正确启动
- python - 如何在 Windows 上安装 pipx?
- splunk - Splunk 搜索 - 如何按组重置统计信息而不是搜索的所有统计信息
- firebase - vue + firestore:如何使用 vuefire 绑定/合并数据?
- apache - 将文件 url 重定向到 .htaccess 中的另一个页面
- javascript - 如何根据通过提示从用户输入收集的输入进行自定义嵌入 - Discord.js
- r - 使用 nls 函数进行模型拟合(变量的不匹配维度)
- php - Laravel pluck, groupBy 自定义日期格式
- javascript - nuxt,一旦登陆页面,有时获取钩子不会调用api而是我得到缓存的数据
- javascript - 是否可以在 JavaScript 中创建一个表格行数组?