gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:54:55 25 4
gpt4 key购买 nike

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

我如何拆分(分解?)utilities 列中的值,并将它们与 utilities 表进行匹配,将具有匹配 ID 的值打印为 spitted值(value)观?

例子:

表:房间
|----------------|----------------|---------------- -|
| ID | 姓名 | 实用程序 |
|----------------|----------------|---------------- -|
| 1 |房间 1 | 1,3,4 |
|----------------|----------------|---------------- -|

表:实用程序
|----------------|----------------|
| ID | 设备 |
|----------------|----------------|
| 1 |演讲者 |
|----------------|----------------|
| 2 |电视 |
|----------------|----------------|
| 3 |智能电视 |
|----------------|----------------|
| 4 |网络摄像头|
|----------------|----------------|

我想打印这样的东西:
房间 1:扬声器、智能电视、网络摄像头

这是我目前所拥有的,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

关于php - SQL从一个表中拆分逗号分隔列表并根据此列表从第二个表中打印内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50344724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com