修改我的SELECT语句以考虑某个Word

时间:2016-01-19 09:02:43

标签: php mysql

如何使SELECT语句检查某个Title是否以单词开头并在列表底部显示?请参阅下面的示例。

<?php
    @$DBn = new mysqli('localhost','REMOVED','REMOVED','REMOVED');

    if (mysqli_connect_errno()) {
        echo 'Cannot connect to database: ' . mysqli_connect_error();
    }

    else {
        $Queryn = "SELECT Title FROM Courses where Status = 'Live' order by Title";
        $Resultn = $DBn->query($Queryn);
        $NumResults = $Resultn->num_rows;

        while ($Rown = $Resultn->fetch_assoc()) {
            echo "<li><a href='http://www.domain.com/course-detail.php?Title=". $Rown['Title'] ."'>". $Rown['Title']  ."</a></li> \n";
        }
    }
?>

以上代码将从课程中选择每个标题并按字母顺序显示。如果课程以&#34; Refresher&#34;这个词开头,我怎么能这样做呢?它会在最后一个课程之后放在底部吗?

课程A, 课程B, 课程C, 课程D, 进修课程A, 进修课程B, 进修课程C, 进修课程D

表结构如下。 Table Structure

1 个答案:

答案 0 :(得分:3)

SELECT Title
FROM Courses
WHERE Status = 'Live'
ORDER BY
    (CASE WHEN Title LIKE 'Refresher%' THEN NULL ELSE Title END) IS NULL ASC,
    Title DESC

按照以下链接进行正在运行的演示:

SQLFiddle