Mysql SQL案例-如果某个值等于某个值,则执行某个操作

Mysql SQL案例-如果某个值等于某个值,则执行某个操作,mysql,sql,Mysql,Sql,这是我的SQL查询的一部分 SELECT c.fullname AS Course, gi.itemname AS Activity, CONCAT(ROUND(gg.finalgrade,1) , '%') AS Grade, cmc.completionstate, gi.itemmodule, concat('<a target="_new" href="localhost/moodle/mod/quiz/view.php?id=',cm.id,'">View th

这是我的SQL查询的一部分

SELECT 

c.fullname AS Course,
gi.itemname AS Activity,
CONCAT(ROUND(gg.finalgrade,1) , '%') AS Grade,   
cmc.completionstate,
gi.itemmodule,

concat('<a target="_new" href="localhost/moodle/mod/quiz/view.php?id=',cm.id,'">View this Activity</a>') AS 'Link'
选择
c、 全名为课程,
gi.itemname作为活动,
混凝土(圆形(最终等级,1),“%”作为等级,
cmc.completionstate,
gi.items模块,
concat(“”)作为“链接”
当gi.itemmodule=quick时,我想显示上面的链接,当gi.itemmodule=scorm时,我想显示另一个链接(…moodle/mod/scorm…)

我认为这需要一个
CASE
语句,但不确定如何进行。

CASE gi.itemmodule
case gi.itemmodule
     when 'quiz' 
       then concat(<onelink>)
     when 'scorm'
       then concat(<oneotherlink>)
     else concat(<defaultlink>)
end as 'Link'
什么时候“测验” 那么concat() 当“scorm” 那么concat() else concat() 以“链接”结束
如果只需更改concat的一部分,也可以在concat中执行此操作

差不多

concat('<a target="_new" href="',
       (case gi.itemmodule
        when 'quiz' 
          then 'value of href'
        when 'scorm'
           then 'another value of href'
        else 'defaultvalue of href'
        end), 
        cm.id,
        '">View this Activity</a>') as 'Link'
concat(“”)作为“链接”
这将避免代码重复,但可能更难阅读

对于给定的示例,您似乎可以这样做(但不确定)

concat(“”)作为“链接”
或者如果某些itemmodule对链接有用,但不是全部

concat('<a target="_new" href="localhost/moodle/mod/',
        (case when gi.itemmodule in ('scorm', 'quiz', 'asdf') 
             then gi.itemmodule
             else 'defaultValue'
        end), 
       '/view.php?id=',
       cm.id,
       '">View this Activity</a>') AS 'Link'
concat(“”)作为“链接”
案例gi.itemmodule
什么时候“测验”
那么concat()
当“scorm”
那么concat()
else concat()
以“链接”结束
如果只需更改concat的一部分,也可以在concat中执行此操作

差不多

concat('<a target="_new" href="',
       (case gi.itemmodule
        when 'quiz' 
          then 'value of href'
        when 'scorm'
           then 'another value of href'
        else 'defaultvalue of href'
        end), 
        cm.id,
        '">View this Activity</a>') as 'Link'
concat(“”)作为“链接”
这将避免代码重复,但可能更难阅读

对于给定的示例,您似乎可以这样做(但不确定)

concat(“”)作为“链接”
或者如果某些itemmodule对链接有用,但不是全部

concat('<a target="_new" href="localhost/moodle/mod/',
        (case when gi.itemmodule in ('scorm', 'quiz', 'asdf') 
             then gi.itemmodule
             else 'defaultValue'
        end), 
       '/view.php?id=',
       cm.id,
       '">View this Activity</a>') AS 'Link'
concat(“”)作为“链接”
案例gi.itemmodule
什么时候“测验”
那么concat()
当“scorm”
那么concat()
else concat()
以“链接”结束
如果只需更改concat的一部分,也可以在concat中执行此操作

差不多

concat('<a target="_new" href="',
       (case gi.itemmodule
        when 'quiz' 
          then 'value of href'
        when 'scorm'
           then 'another value of href'
        else 'defaultvalue of href'
        end), 
        cm.id,
        '">View this Activity</a>') as 'Link'
concat(“”)作为“链接”
这将避免代码重复,但可能更难阅读

对于给定的示例,您似乎可以这样做(但不确定)

concat(“”)作为“链接”
或者如果某些itemmodule对链接有用,但不是全部

concat('<a target="_new" href="localhost/moodle/mod/',
        (case when gi.itemmodule in ('scorm', 'quiz', 'asdf') 
             then gi.itemmodule
             else 'defaultValue'
        end), 
       '/view.php?id=',
       cm.id,
       '">View this Activity</a>') AS 'Link'
concat(“”)作为“链接”
案例gi.itemmodule
什么时候“测验”
那么concat()
当“scorm”
那么concat()
else concat()
以“链接”结束
如果只需更改concat的一部分,也可以在concat中执行此操作

差不多

concat('<a target="_new" href="',
       (case gi.itemmodule
        when 'quiz' 
          then 'value of href'
        when 'scorm'
           then 'another value of href'
        else 'defaultvalue of href'
        end), 
        cm.id,
        '">View this Activity</a>') as 'Link'
concat(“”)作为“链接”
这将避免代码重复,但可能更难阅读

对于给定的示例,您似乎可以这样做(但不确定)

concat(“”)作为“链接”
或者如果某些itemmodule对链接有用,但不是全部

concat('<a target="_new" href="localhost/moodle/mod/',
        (case when gi.itemmodule in ('scorm', 'quiz', 'asdf') 
             then gi.itemmodule
             else 'defaultValue'
        end), 
       '/view.php?id=',
       cm.id,
       '">View this Activity</a>') AS 'Link'
concat(“”)作为“链接”

您不应该在SQL查询中真正构建链接。请改用moodle_url()函数

$sql = "SELECT 
            c.fullname AS Course,
            gi.itemname AS Activity,
            " . $DB->sql_concat('ROUND(gg.finalgrade,1)' , '%') . " AS Grade,   
            cmc.completionstate,
            gi.itemmodule,
            ...
        ";
$DB->sql_concat()是Moodle中用于使用concat的跨数据库函数

$activities = $DB->get_records_sql($sql);
foreach ($activities as $activity) {
    if ($activity->itemmodule == 'quiz') {
        // Custom url
        $url = new moodle_url('/mod/quiz/view.php', array('id' => $activity->cmid));
    } else {
        // Module url
        $url = new moodle_url('/mod/' . $activity->itemmodule . '/view.php', array('id' => $activity->cmid));
    }
    $link = html_writer::link($url, get_string('view', 'mod_' . $activity->itemmodule));
    echo $link;
}

您不应该在SQL查询中真正构建链接。请改用moodle_url()函数

$sql = "SELECT 
            c.fullname AS Course,
            gi.itemname AS Activity,
            " . $DB->sql_concat('ROUND(gg.finalgrade,1)' , '%') . " AS Grade,   
            cmc.completionstate,
            gi.itemmodule,
            ...
        ";
$DB->sql_concat()是Moodle中用于使用concat的跨数据库函数

$activities = $DB->get_records_sql($sql);
foreach ($activities as $activity) {
    if ($activity->itemmodule == 'quiz') {
        // Custom url
        $url = new moodle_url('/mod/quiz/view.php', array('id' => $activity->cmid));
    } else {
        // Module url
        $url = new moodle_url('/mod/' . $activity->itemmodule . '/view.php', array('id' => $activity->cmid));
    }
    $link = html_writer::link($url, get_string('view', 'mod_' . $activity->itemmodule));
    echo $link;
}

您不应该在SQL查询中真正构建链接。请改用moodle_url()函数

$sql = "SELECT 
            c.fullname AS Course,
            gi.itemname AS Activity,
            " . $DB->sql_concat('ROUND(gg.finalgrade,1)' , '%') . " AS Grade,   
            cmc.completionstate,
            gi.itemmodule,
            ...
        ";
$DB->sql_concat()是Moodle中用于使用concat的跨数据库函数

$activities = $DB->get_records_sql($sql);
foreach ($activities as $activity) {
    if ($activity->itemmodule == 'quiz') {
        // Custom url
        $url = new moodle_url('/mod/quiz/view.php', array('id' => $activity->cmid));
    } else {
        // Module url
        $url = new moodle_url('/mod/' . $activity->itemmodule . '/view.php', array('id' => $activity->cmid));
    }
    $link = html_writer::link($url, get_string('view', 'mod_' . $activity->itemmodule));
    echo $link;
}

您不应该在SQL查询中真正构建链接。请改用moodle_url()函数

$sql = "SELECT 
            c.fullname AS Course,
            gi.itemname AS Activity,
            " . $DB->sql_concat('ROUND(gg.finalgrade,1)' , '%') . " AS Grade,   
            cmc.completionstate,
            gi.itemmodule,
            ...
        ";
$DB->sql_concat()是Moodle中用于使用concat的跨数据库函数

$activities = $DB->get_records_sql($sql);
foreach ($activities as $activity) {
    if ($activity->itemmodule == 'quiz') {
        // Custom url
        $url = new moodle_url('/mod/quiz/view.php', array('id' => $activity->cmid));
    } else {
        // Module url
        $url = new moodle_url('/mod/' . $activity->itemmodule . '/view.php', array('id' => $activity->cmid));
    }
    $link = html_writer::link($url, get_string('view', 'mod_' . $activity->itemmodule));
    echo $link;
}

感谢Raphael,这看起来很棒,但我不仅有两个选项,还有“测验”、“scorm”、“作业”等不同的值。总共大约有5个值。那就不需要稍微不同了吗?thanks@user1525612我换了两个以上的例子。顺便说一句,添加了最后一个解决方案,可以使事情变得更简单(但这取决于您的数据/需求)。感谢Raphael。这看起来很棒,但我不仅有两个选项,还有“测验”、“scorm”、“作业”等不同的值。总共大约有5个值。那就不需要稍微不同了吗?thanks@user1525612我换了两个以上的例子。顺便说一句,添加了最后一个解决方案,可以使事情变得更简单(但这取决于您的数据/需求)。感谢Raphael。这看起来很棒,但我不仅有两个选项,还有“测验”、“scorm”、“作业”等不同的值。总共大约有5个值。那就不需要稍微不同了吗?thanks@user1525612我换了两个以上的例子。顺便说一句,添加了最后一个解决方案,可以使事情变得更简单(但这取决于您的数据/需求)。感谢Raphael。这看起来很棒,但我不仅有两个选项,还有“测验”、“scorm”、“作业”等不同的值。总共大约有5个值。那就不需要稍微不同了吗?thanks@user1525612我换了两个以上的例子。顺便说一句,添加了最后一个解决方案,可以使事情变得更简单(但这取决于您的数据/需求)。嗨,Russel,谢谢!不过,我将自定义sql报表插件与可配置报表结合使用。你好,Russel,谢谢!不过,我将自定义sql报表插件与可配置报表结合使用。你好,Russel,谢谢!不过,我将自定义sql报表插件与可配置报表结合使用。你好,Russel,谢谢!不过,我将自定义sql报表插件与可配置报表结合使用。