我有这三个表,分别代表三个猫系列在各个主题中的表现:
CAT1
adm_no eng mat bio che phy
1111 90 87 89 74 92
2222 65 87 54 65 87
3333 96 95 98 58 56
4444 58 28 88 74 85
CAT2
adm_no eng mat bio che phy
1111 40 82 81 79 90
2222 67 88 78 67 84
3333 97 99 95 24 78
4444 82 22 83 76 90
CAT3
adm_no eng mat bio che phy
1111 80 45 86 74 96
2222 95 87 56 65 83
3333 46 97 84 58 87
4444 78 23 80 74 83
我想了解每个学生在科目方面的表现趋势。例如,如果我希望获得eng的性能趋势,那么结果将是这样的:
adm_no CAT1 CAT2 CAT3
1111 90 40 80
2222 65 67 95
3333 96 97 46
4444 58 82 78
我将如何使用mysql和php来实现这一目标?其次,也是最重要的是,如果表的数量不是静态的,那将如何实现?(可能是两个甚至四个)。
我假设您将拥有至少cat1。该查询基本上是scaisEdge在其答案中提供的内容。但是,这将为您提供一个如何使用PHP动态生成它的想法。下面的函数将构建查询,您可以使用该查询来获得所需的结果。
function buildQuery($subject) {
$select .= "select `cat1`.adm_no, `cat1`.`$subject` as CAT1";
$from = ' from cat1';
$i = 2;
while (mysql_num_rows(mysql_query("SHOW TABLES LIKE 'cat$i'"))) {
$table = "cat$i";
$label = "CAT$i";
$previousTable = 'cat'.($i-1);
$select .= ", `$table`.`$subject` as $label";
$from .= " inner join $table on `$table`.adm_no = `$previousTable`.adm_no ";
$i++;
}
return $select.$from;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句