dsuw85815 2014-04-04 10:56
浏览 167
已采纳

MySql COALESCE无法正常工作

I have this table, 2 courses with 12 possible subjects (content: subject ID, or NULL if no more subjects exsist). Please note that it can contain up to 50 different courses, not only 2

table

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:

Curso de Programación Creativa con PHP y MySQL - CURSO: 1 - CURSO: 2 - CURSO: 10 -

Máster en Diseño y Desarrollo Web - CURSO: 1 -


But something is failing as with my COALESCE expression I get:

Curso de Programación Creativa con PHP y MySQL - CURSO: 1 - CURSO: 1 -

Máster en Diseño y Desarrollo Web - CURSO: 1 - CURSO: 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>';
}
  • 写回答

1条回答 默认 最新

  • douxi3404 2014-04-04 11:07
    关注

    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)
    • the second SQL-query does not include any WHERE-clause. Because of this you get co_subj1 of all courses

    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>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体