duanji1043 2012-10-06 08:59
浏览 85
已采纳

如果其中一个字段被分组,则不获取表中其他字段的所有结果

I am grouping the model field in cars table ,but i am getting only one,one result ,istead of all , from each differend grouped by models.

suppose there are three 1990,19190,1990 under moldel field and three cars suzuki,honda,tyota are undel car_name whose model is 1990 .but I am getting results of only one car name under 1990 model. Plz help me.

         $sql=mysql_query("SELECT * FROM cars group by  model ");
    while ($row=mysql_fetch_array($sql)) {
       $car_name=$row['car_name'];
       $model=$row['model'];


            ?>

    <table>

        <tr>
            <td><?php  echo $model ?></td>   //here is displaying  1990.

        </tr >
        <tr  >
              <td> <?php   echo $car_name;  ?></td>    // but here is displayed only onr car name istead of all.

        </tr>

    </table>



    <?php

      }
  • 写回答

2条回答 默认 最新

  • doujiang9887 2012-10-06 09:28
    关注

    When you group by a particular column, you are asking for just one row for each value of that column. If you want 1990 Honda and also 1990 Suzuki, you should use GROUP BY car_name, model to get all the unique combinations of this pair of columns.

    BTW, does your model column contain years? Car models are names like Accord, Camry, Celica.

    EDIT:

    I think this is what you're looking for:

    SELECT model, GROUP_CONCAT(car_name)
    FROM cars
    GROUP BY model
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line