PHP 结果 MYSQL 多对多 table 和 GROUP By

PHP results with MYSQL many to many table and GROUP By

我正在汇总一份服务提供商列表。我希望它们像这样列出:

管道工: 乔的管道 迈克的管道

电工: 斯科特电气 约翰电

等...

如何从我的查询中提取和分组这些信息。另外,当我尝试调用 pt.name 时,它说它是 NULL,为什么?

谢谢

$qry  = "SELECT pt.name, p.name, p.phone
FROM provider_type AS pt
LEFT JOIN providers_provider_type AS ppt ON pt.id = ppt.provider_type_id
LEFT JOIN providers AS p ON ppt.providers_id = p.id
WHERE installation_id = $installation_id
GROUP BY ppt.provider_type_id";
//$qry = "SELECT * FROM providers WHERE installation_id = $installation_id";    
$res = mysqli_query($mysqli, $qry) or die('-1'.mysqli_error($mysqli));


    $categories = array();
    while ($row_rsCategories = mysqli_fetch_assoc($res)) { 


/////////////////////////////////////////////// 

        $categories[] = array(
            'id' => $row_rsCategories['id'],
            'provider_type' => $row_rsCategories['pt.name'],
            'provider_name' => $row_rsCategories['name']
        );

    }


?>


<div class="container">
    <div class="content">
        <?php if ($msgBox) { echo $msgBox; } ?>
        <div class="row">  
<?php var_dump($categories); ?>

<?php if (empty($categories)) { ?>
    <div class = "big-icon">
        <i class="fa fa-meh-o fa-5x"></i>
    </div>
    <p><center>Sorry! It looks like we don't have a Scout in this area. <br />
        Please check back in as we continue to add new Scouts to the network all the time.</center></p>

<?php  } else {
?>

<table class="table table-striped">
<tr>
    <th>Type</th>
    <th>Name</th>
    <th>Phone</th>
    <th>More Info</th>
</tr>

<?php foreach ($categories as $category) { ?>
<tr>
    <td><?php echo $category['provider_type']; ?>  </td>
    <td><?php echo $category['provider_name']; ?> </td>
    <td> </td>
    <td> </td>
</tr>   

    <?php  } 
        }
        ?>  
</table>    

您的查询只返回每种提供商类型的一行,而您想要列出所有提供商(每种类型不止一个)。

一种选择是使用 GROUP_CONCAT,它将每种类型的所有提供程序连接成一个逗号分隔的字符串:

SELECT pt.name AS type, GROUP_CONCAT(p.name) AS providers
FROM provider_type AS pt
LEFT JOIN providers_provider_type AS ppt ON pt.id = ppt.provider_type_id
LEFT JOIN providers AS p ON ppt.providers_id = p.id
WHERE installation_id = $installation_id
GROUP BY ppt.provider_type_id;

这将为您提供如下所示的结果集:

type        | providers                       |
------------+---------------------------------+
plumber     | Joe's Plumbing,Mike's Plumbing  |
electrician | Scott electric,John electric    |

在你的情况下,我认为有一个更好的选择 - 因为无论如何你都将所有结果检索到 php,你可能想要一个一个地获取所有提供者,然后在中进行分组您的代码,并以您喜欢的任何方式显示它。

查询将与您的查询相同,但没有分组:

SELECT pt.name AS type, p.name, p.phone
FROM provider_type AS pt
LEFT JOIN providers_provider_type AS ppt ON pt.id = ppt.provider_type_id
LEFT JOIN providers AS p ON ppt.providers_id = p.id
WHERE installation_id = $installation_id;

结果集将只是一个提供者列表,每个提供者都有他的名字和 phone 编号。

type        | name            | phone         |
------------+-----------------+---------------+
plumber     | Joe's Plumbing  | 123...        |
plumber     | Mike's Plumbing | 123...        |
electrician | Scott electric  | 123...        |
electrician | John electric   | 123...        |

至于NULL在pt.name—— 在 MySQL 的结果集中,您没有得到 pt.namep.name 作为列名。您得到 namename1(两列具有相同的名称,无论它们最初来自哪个 table,因此数据库只是添加 1 以具有唯一名称)。

您应该在查询中为您的列指定唯一的名称,例如pt.name AS type 然后使用这些名称来读取数据,例如'provider_type' => $row_rsCategories['type']