数据表是这样的:
id | 分类 | 标题
1    a        11111
2    a        2222222222
3    a        3333333333
4    b        444444444
5    b        555555555
输出结果应该是
<select>
<optgroup label="a">
  <option>11111</option>
  <option>22222222</option>
  <option>33333</option>
</optgroup><optgroup label="b">
  <option>444444</option>
  <option>55555555</option>
</optgroup>
</select>如何输出?

解决方案 »

  1.   

    select 分类,group_concat(标题) from t group by 分类;这样查询后就不难了吧
      

  2.   

    不是,,,sql语句已经写出来了,这个表实际上是查询的结果集:
    id | 分类 | 标题
    1    a        11111
    2    a        2222222222
    3    a        3333333333
    4    b        444444444
    5    b        555555555
      

  3.   

    不用GROUP_CONCAT可以吗。因为标题比较长,数据量可能会很大!
      

  4.   

    还不如我那样写来的简单,话说你也贴点你的php代码呀
      

  5.   

    echo '<select>';
    $last = '';
    while*$r = mysql_fetch_assoc($rs)) {
      if($r['分类'] != #last) {
        if($last) echo '</optgroup>';
        echo "<optgroup label='$r[分类]'>";
        $last = $r['分类'];
      }
      echo "<option>$r[标题]</option>";
    }
    echo '</optgroup>';
    echo '</select>';
      

  6.   

    #last
    *$r
    是什么意思?
      

  7.   

    不好意思,手误:)echo '<select>';
    $last = '';
    while($r = mysql_fetch_assoc($rs)) {
      if($r['分类'] != $last) {
        if($last) echo '</optgroup>';
        echo "<optgroup label='$r[分类]'>";
        $last = $r['分类'];
      }
      echo "<option>$r[标题]</option>";
    }
    echo '</optgroup>';
    echo '</select>';