日期:2014-05-16  浏览次数:21082 次

mysql:如何把第一列和第二列一样的多行数据合并成一行?
为了方便看结果,各位请把下面的代码拷贝到html页面就知道我要问什么了?
只是这里无法显示出表格来,麻烦各位!
HTML code
<table width="418" border="1">
  <tr>
    <td width="48">teacher </td>
    <td width="68">class_time</td>
    <td width="52">t900    </td>
    <td width="52">t930 </td>
    <td width="39">t100</td>
    <td width="36">t1030</td>
    <td width="41">t1100</td>
  </tr>
  <tr>
    <td>A       </td>
    <td>2012-5-14 </td>
    <td>班级A</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>A </td>
    <td>2012-5-14 </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>班级B</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>A </td>
    <td>2012-5-14 </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>班级C</td>
  </tr> <tr>
    <td>B</td>
    <td>2012-5-14 </td>
    <td>&nbsp;</td>
    <td>班级D</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>B</td>
    <td>2012-5-14 </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>班级E</td>
    <td>&nbsp;</td>
  </tr>
</table>
<p>合并成</p>
<table width="418" border="1">
  <tr>
    <td width="48">teacher </td>
    <td width="68">class_time</td>
    <td width="52">t900 </td>
    <td width="52">t930 </td>
    <td width="39">t100</td>
    <td width="36">t1030</td>
    <td width="41">t1100</td>
  </tr>
  <tr>
    <td>A </td>
    <td>2012-5-14 </td>
    <td>班级A</td>
    <td>&nbsp;</td>
    <td>班级B</td>
    <td>&nbsp;</td>
    <td>班级C</td>
  </tr>
  <tr>
    <td>B</td>
    <td>2012-5-14 </td>
    <td>&nbsp;</td>
    <td>班级D</td>
    <td>&nbsp;</td>
    <td>班级E</td>
    <td>&nbsp;</td>
  </tr>
 
</table>


------解决方案--------------------
SQL code
select teacher,class_time,group_concat(t900 SEPARATOR ''),group_concat(t930 SEPARATOR ''),group_concat(t930 SEPARATOR ''),group_concat(t100 0SEPARATOR ''),group_concat(t1030 SEPARATOR ''),group_concat(t1100 SEPARATOR '')
from tb
group by teacher,class_time