dongwei3866 2017-01-06 05:08
浏览 38
已采纳

如何在mysql左连接中删除相同的名称出现?

I am using the following query.

SELECT DISTINCT ph.module_head_id,
  ph.module_head, pm.module_id, 
  pm.module_name, pm.module_url
FROM pms_module_header ph
LEFT JOIN pms_module pm
ON ph.module_head_id = pm.module_head_id

Now i am getting the following output

Module_head_id  Module_head module_id module_name module_url
  1               dashboard     Null    Null        Null
  2                Employee      1      Add        test.php
  2                Employee      2      View       test1.php
  3               Report         3      Project    project.php
  3               Report         4      Client     client.php

How to remove the same name occurences in module_head

I Want to display the out like these in codeigniter view

dashboard
  Employee Add 
           View
  Report   Project
           Client

I am using array_unique function but its not work.Please help me

  • 写回答

1条回答 默认 最新

  • donglan6967 2017-01-06 05:10
    关注

    Here is the query,

    SELECT DISTINCT ph.module_head_id,
      ph.module_head, pm.module_id, 
      pm.module_name, pm.module_url
    FROM pms_module_header ph
    LEFT JOIN pms_module pm
    ON ph.module_head_id = pm.module_head_id
    group by ph.module_head_id
    

    Just apply group by on module_head_id, it will work.

    EDIT

    <table border="1" width="100%">
        <tr>
            <th>Module head</th>
            <th>Module name</th>
        </tr>
        <?php
        $module_head = '';
        foreach ($arr as $k => $v) {
            echo "<tr>";
            echo "<td>";
            if ($v['module_head'] == '' || $v['module_head'] != $module_head) {
                $module_head = $v['module_head'];
                echo $v['module_head'] . "<br/>";
            } else {
                echo " ";
            }
            echo "<td>";
            echo "<td>" . $v['module_name'] . "</td>";
            echo "<tr>";
        }
        ?>
        <table>
    

    Manipulate as per your view. It will work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿