2010-11-03 117 views
11

我有一个表“exercise_results”。人们在一开始就投入了他们的成果,然后两个月后他们把结果放到了他们的改进之中。他们的开始集具有“1”的exercise_type_id,并且结束集具有“2”的exercise_type_id。如何将两个MySQL行合并为一个并使用PHP将它们显示在表中?

我需要一种方法来显示这一点成HTML表格,看起来像这样:

Normally I do this using

foreach循环,但是这与单排。我无法将两行合并为一行。我认为这可能与某种MySQL加入一样简单?我们通过他们的person_unique_id标识每个人。

这里是我的领域:

id | person_unique_id | person_name | exercise_type_id | mile_running_time | bench_press_weight_lbs | squat_weight_lbs | date_of_exercise_performed 

示例行:

1 | J123 | John Smith | 1 | 8 | 200 | 300 | 2010-03-20 
2 | J123 | John Smith | 2 | 7 | 250 | 400 | 2010-05-20 
3 | X584 | Jane Doe | 1 | 10 | 100 | 200 | 2010-03-20 
4 | X584 | Jane Doe | 2 | 8 | 150 | 220 | 2010-05-20 

我已经尝试了一些解决方案,但我迷路了。任何帮助都会很棒。谢谢!

编辑:

针对下面的评论,我希望像一些数据:

array 0 => 
array 
    'Exercise' => 
    array 
     'person_unique_id' => string 'J123' 
     'person_name' => string 'John Smith' 
     'begin_mile_running_time' => string '8' 
     'end_mile_running_time' => string '7' 
1 => 
array 
    'Exercise' => 
    array 
     'person_unique_id' => string 'X584' 
     'person_name' => string 'Jane Doe' 
     'begin_mile_running_time' => string '10' 
     'end_mile_running_time' => string '8' 
+0

数据库是否修复?我的意思是,你能改变桌子吗? – 2010-11-03 20:03:24

+0

不是。生产应用程序中使用的表中已经有很多数据。 – Michael 2010-11-03 20:04:46

+0

您可以通过按id分组并查询,并选择MAX(IF(exercise_type_id = 1,mile_running_time,''))AS'Start',MAX(IF(exercise_type_id = 2,mile_running_time,''))AS'End '等...可能有点具有挑战性,但我只需要做这种类型的汇总。 – methodin 2010-11-03 20:18:59

回答

17

您可以使用GROUP_CONCAT()得到两行的结果是这样的:

SELECT person_unique_id, person_name, 
     group_concat(mile_running_time) AS miles, 
     group_concat(bench_press_weight_lbs) AS bench, 
     GROUP_CONCAT(squat_weight_lbs) AS squat 
FROM exercise_result 
GROUP BY person_unique_id 

你的结果会是这样的:

J123 | John Smith | 8,7 | 200,250 | 300,400 

X584 | Jane Doe | 10,8 | 100,150 | 200,220 

然后你就可以使用PHP,结果场爆炸让每种类型的结果。

+0

这对我很有用。谢谢! – Michael 2010-11-03 20:55:08

+0

很高兴有这样一个简单的解决方案,但它是非常严格的。如果你有任何带有逗号的字段(或者如果MySQL支持逗号作为十进制标记?),这将不起作用。至少使用像管道这样不常见的字符:| – theazureshadow 2010-11-03 21:02:28

+0

你可以像这样在你的group_concat中指定你想要的分隔符:group_concat(mile_running_time SEPARATOR'#') – layalk 2010-11-03 21:11:41

0
$query = mysql_query("select ...your data"); 

while ($row = mysql_fetch_assoc ($query)) { 
    if ($row['exercise_type_id']==1) 
     $exe1[]=$row; 
    else 
     $exe2[]=$row; 


} 

print_r($exe1); 
print_r($exe2); 

从我所了解

编辑:

试试这个

$query = mysql_query("select ...your data"); 

while ($row = mysql_fetch_assoc ($query)) { 

    $rows[]=array('Exercise'=>$row); 


} 

print_r($rows); 
+0

这段代码不会在每个人的HTML表格中创建两行吗?第一行是他们的BEGIN数据,第二行是他们的END? – Michael 2010-11-03 20:07:07

+0

我试图让你的想法工作,但有困难。我认为问题出在我的查询中。谢谢。 – Michael 2010-11-03 20:58:25

0

提取整个表格或任何您感兴趣的行,对person id进行排序,将每行的人员id与下一个人员的id进行比较,以查看是否存在打印HTML表格中所有列的结果。如果没有,跳到下一个,并将这些字段留空(或其他解决方案,可能忽略没有填写两个字段的人员?)。

我的PHP技能是有限的,所以没有代码示例。

+0

感谢您的想法。 – Michael 2010-11-03 20:57:50

0

如果您订购person_unique_id,然后exercise_type_id,您可以这样做。如果你有两行给大家,你可以离开了,如果(只有使用人):

for($i = 0; $i < count($exercise_results); $i++) 
{ 
    $first = $exercise_results[$i]; 
    if(!isset($exercise_results[$i+1]) 
     || $first['person_unique_id'] != $exercise_results[$i+1]['person_unique_id') { 
    $second = array(
     'person_name'=>$other['person_name'], 
     'mile_running_time' => null // fill in the rest with defaults (like null) 
    ); 
    } else { 
    $second = $exercise_results[$i+1]; 
    $i++; // skip ahead one, since you've already used the second result. 
    } 
    // perform your normal printing, but use $beginning and $end to get the respective results 
} 
+0

感谢您的信息。似乎它会工作。 – Michael 2010-11-03 20:56:05

相关问题