doumouyi4039 2017-05-23 08:29
浏览 19
已采纳

如何从mysql计数多个值作为html和php中的一列

$query="SELECT data_home.name_home, flower.flower FROM
data_home INNER JOIN (flower INNER JOIN home_flower ON flower.id_flower=home_flower.id_flower)
ON data_home.id_home = home_flower.id_home";

$data=mysql_query($query);
            $no=0;

            //output
            while ($row=mysql_fetch_array($data))
            {
                $no++;
                if (empty($row['foto_home']))
                {
                    $gambar = "default2.jpg";
                }
                else
                {
                    $gambar = $row['foto_home'];
                }
                echo "<tr class='odd gradeA'>
                        <td width='5%'>".$no."</td>
                        <td width='20%'>".$row['name_home']."</td>
                        <td width='25%'>".$row['home_adress']."</td>
                        <td width='15%'>".$row['flower']."</td>
                        <td width='15%'><img src='uploads/".$gambar."' width='125px' height='125px'></img></td>
                        <td width='20'>
                            <center> 
                                <a href='?restore=".$row['id_home']."' onclick='return confirmRestore()'>
                                    <button type='button' class='btn btn-warning btn-circle' title='Restore'><i class='icon-refresh'></i><font size='1px'> Restore</font></button>
                                </a> 
                                <a href='?delete=".$row['id_home']."' onclick='return confirmSubmit()'>
                                    <button type='button' class='btn btn-danger btn-circle' title='Hapus'><i class='icon-trash'></i><font size='1px'> Delete</button></font>
                                </a>
                            </center>
                        </td>
                    </tr>
                ";
            }
            ?>

i have code like this for my program and The output I want is as follows:

no             home            flower
1             type 41         tulip, rose, lily

but i dont knnow why when i running the program the out come be like this:

no            home            flower
1             type 41         tulip
2             type 41         rose
3             type 41         lily 

i am really new to php and html so i dont have idea how to fix this.my question how to arrange it so "flower" with same "home" just count 1 and just seperate flower in one column with "," ?.thnx and sory for my bad english and for mysql code iknow ppl today using mysqli but this is the old program.

  • 写回答

2条回答 默认 最新

  • dongshi1148 2017-05-23 08:38
    关注

    Your expected output in SQL could be achieved by this query, which uses GROUP_CONCAT function of MYSQL and gives you comma separated flowers, aggregated by home

    But if you current query you haven't mentioned about column no.

    SELECT dh.name_home, group_concat(f.flower) as flower
         FROM
    data_home dh 
           INNER JOIN 
         home_flower hf
    ON dh.id_home = hf.id_home
    INNER JOIN 
           flower f
      ON f.id_flower=hf.id_flower
    group by dh.name_home
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM