PHP结果与MYSQL有很多对很多表和GROUP By

时间:2015-02-13 19:00:43

标签: php mysql many-to-many

我正在整理服务提供商列表。我希望它们被列为:

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

电工: 斯科特电动 约翰电气

等等......

如何从查询中提取和分组此信息。此外,当我尝试调用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>    

1 个答案:

答案 0 :(得分:0)

您的查询每个提供程序类型只返回一行,而您希望列出所有提供程序(每种类型都有多个)。

一种选择是使用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;

结果集只是一个提供者列表,每个提供者都有他的姓名和电话号码。

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

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

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