GROUP_CONCAT未按预期返回输出

时间:2015-03-27 18:45:07

标签: php html mysql

我有一个页面,我可以使用称为“标签”的东西查看所有“建议”,类似于堆栈溢出标签的概念。在使用Group_Concat之前,为每个为该提案输入的新标记添加了表中的新行,这会产生大量重复数据,如下所示:

table showing duplicate rows

即使在使用distinct之后它也会显示相同的行,因为标记不同。我试图让这些标签存在于同一列中,因此它就像电子商务,“个性化”等,并将其显示在一行中。我被告知GROUP_CONCAT将是解决方案但是在使用之后,它似乎返回一行包含所有标签,如下所示:

All tags in one row

我真的不确定如何克服这个问题。任何帮助将不胜感激。

这是sql查询的代码:

$stmt = $db_conx->prepare('SELECT p.proposal_id, p.proposal_title, p.description, c.course_title, GROUP_CONCAT(DISTINCT t.tag_title)  FROM proposal p LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id LEFT JOIN tag_details t on t.tag_code = pt.tag_code LEFT JOIN course_details c on c.course_code = p.course_code');
    $stmt->execute();

    $proposals = $stmt->fetchAll(PDO::FETCH_ASSOC);

这是表格:

  <h4><center>View All Proposals</center></h4>
  <div class = "container container-fluid"id = "table_container" style="width:auto; margin-top:50px;" class="mainbox col-md-6">
    <div class="row clearfix">
      <div class="col-md-12">
        <table class="table table-bordered table-hover" id="tab_logic">
          <thead>
            <tr >
              <th class="text-center">
                Proposal ID
              </th>
              <th class="text-center">
                Proposal Title
              </th>
              <th class="text-center">
                Proposal Description
              </th>
              <th class="text-center">
                Course
              </th>
              <th class="text-center">
                Tags
              </th>
            </tr>
          </thead>
          <tbody>
            <!-- populating the table with information from mysql database -->
            <?php foreach ($proposals as $proposal) {
              echo "<tr><td>";
              echo $proposal['proposal_id'];
              echo "</td><td>";
              echo substr($proposal['proposal_title'],0,30) ."...";
              echo "</td><td>";
              echo substr($proposal['description'],0,50) ."...";
              echo "</td><td>";
              echo $proposal['course_title'];
              echo "</td><td>";
              echo $proposal['GROUP_CONCAT(DISTINCT t.tag_title)'];
              echo "<tr><td>";
</tbody>
              </table>
            </div>
          </div>
        </div>

第二次尝试查询:

('SELECT p.proposal_id, p.proposal_title, p.description, c.course_title, pt.*, GROUP_CONCAT(DISTINCT t.tag_title) AS tags  FROM proposal p 
      LEFT JOIN course_details c on c.course_code = p.course_code
      LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
      LEFT JOIN tag_details t on t.tag_code = pt.tag_code
      WHERE pt.tag_code IN ('.implode(',', $tag).')
      GROUP BY p.proposal_id ');

1 个答案:

答案 0 :(得分:2)

您需要在查询结尾添加GROUP BY p.proposal_id

否则,MySQL只会将所有结果分组到一行。

SELECT p.proposal_id, p.proposal_title, p.description, c.course_title,
    GROUP_CONCAT(DISTINCT t.tag_title) AS tags
FROM proposal p
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN course_details c on c.course_code = p.course_code
GROUP BY p.proposal_id

P.S。您可以执行GROUP_CONCAT(DISTINCT t.tag_title) AS tags,然后执行$proposal['tags']