我有一个表,它包含属于各个用户的多行数据。例如:
regno course score session semester
03/01/02 Mth111 60 2 3
03/01/02 MTH222 50 2 3
03/04/05 MTH333 40 2 3
03/04/05 MTH111 30 2 3
我希望特定课程和学期的值以这种方式显示在每个regno的一行中:
regno
03/01/02 MTH111 MTH222
60 50
03/04/05 MTH333 MTH111
40 30
我试着使用groupby…,使用带有mysql_fetch_assoc的foreach循环来获取列标题和groupby子句。。。但只显示了一行。我该如何解决这个问题。谢谢
您可以使用以下
select regno, concat(course,''n',score) as someName from table1
或
select regno, concat(course,'<br>',score) as someName from table1
如果显示为HTML。
编辑:对,我明白你现在的意思了。
select regno, group_concat(concat(course, '|', score)) as courses from table1 group by regno
SQLFiddle 的结果
03/01/02 Mth111|60,MTH222|50
03/04/05 MTH333|40,Mth111|30
另一个解决方案是将这些记录拉入程序中,并按照需要的方式进行格式化。
这样以后会更容易理解和调试。