[英]MySql COALESCE not working
I have this table, 2 courses with 12 possible subjects (content: subject ID, or NULL if no more subjects exsist). 我有这张桌子,有2门课程,有12个可能的科目(内容:科目ID,如果没有其他科目,则为NULL)。 Please note that it can contain up to 50 different courses, not only 2
请注意,它最多可以包含50个不同的课程,而不仅仅是2个
I am trying by a WHILE to show course name (workis fine) and by COALESCE to show in each course the course's subjects ID, this should be the final result: 我正在尝试通过WHILE显示课程名称(工作正常),并通过COALESCE在每门课程中显示课程的科目ID,这应该是最终结果:
Curso de Programación Creativa con PHP y MySQL - CURSO: 1 - CURSO: 2 - CURSO: 10 - 用MySQL和MySQL创建程序集-光标:1-光标:2-光标:10-
Máster en Diseño y Desarrollo Web - CURSO: 1 - MásterenDiseñoy Desarrollo网站-CURSO:1-
But something is failing as with my COALESCE expression I get: 但是我得到的COALESCE表达式出现了一些问题:
Curso de Programación Creativa con PHP y MySQL - CURSO: 1 - CURSO: 1 - 使用PHP和MySQL进行编程创作-Curs:1-Curs:1-
Máster en Diseño y Desarrollo Web - CURSO: 1 - CURSO: 1 - MásterenDiseñoy Desarrollo网站-光标:1-光标:1-
Here is the code: 这是代码:
$select = select("SELECT * FROM course_conf JOIN course_type ON ct_id=co_fk_ct_id ORDER BY co_name");
while($registroBbdd = consultaRegistro($select))
{
$courseName=$registroBbdd['co_name'];
$result = select("SELECT COALESCE(co_subj1,co_subj2,co_subj3,co_subj4,co_subj5,co_subj6,co_subj7,co_subj8,co_subj9,co_subj10,co_subj11,co_subj12) FROM course_conf");
echo '<div class="contentColumn80">
<span class="tableContentText ">'.$courseName.' - </span>';
while($row=mysql_fetch_array($result))
{
echo '<span>CURSO: '.$row['COALESCE(co_subj1,co_subj2,co_subj3,co_subj4,co_subj5,co_subj6,co_subj7,co_subj8,co_subj9,co_subj10,co_subj11,co_subj12)'].' - </span>';
}
echo '</div>';
}
OK, there are two obvious errors: 好的,有两个明显的错误:
COALESCE()
will always return the first param, which is not null (in your case always the value stored in co_subj1
) COALESCE()
将始终返回第一个参数,该参数不为null(在您的情况下始终为co_subj1
存储的值) WHERE
-clause. WHERE
。 Because of this you get co_subj1
of all courses co_subj1
While probably not the best solution possible, this should work: 虽然可能不是最好的解决方案,但这应该可行:
$select = select("SELECT * FROM course_conf JOIN course_type ON ct_id=co_fk_ct_id ORDER BY co_name");
while($registroBbdd = consultaRegistro($select))
{
$courseName = $registroBbdd['co_name'];
echo '<div class="contentColumn80">
<span class="tableContentText ">'.$courseName.' - </span>';
for($i = 1; $i <= 12; $i++)
{
if($registroBbdd['co_subj'.$i] != null) {
echo '<span>CURSO: ' . $registroBbdd['co_subj'.$i] . ' - </span>';
}
}
echo '</div>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.