我正在使用sqlite3作为电影目录的本地数据库。它按字母顺序显示。一切都很好但是我想忽略这个词""在电影片名开头显示时。举例来说,如果数据库中的标题是" The Great Gatsby"而不是按字母顺序在T下显示,我希望它在G下列出。所有结果都列在一页上。他们是对DB的查询,可以按照这样的顺序返回结果吗?
$db = new SQLite3('Movie Database.db');
$results = $db->query("SELECT * FROM Movies");
$list = '';
while($row = $results->fetchArray()) {
$id = $row["id"];
$MovieTitle = $row["MovieTitle"];
$MovieYear = $row["MovieDate"];
$MovieRes = $row["MovieRes"];
$FileName = $row["FileName"];
$Summary = $row["Summary"];
$Genres = $row["Genres"];
$PictureLocation = $row["PictureLocation"];
$Rating = $row["Rating"];
$ReleaseDate = $row["ReleaseDate"];
//BUILD THE MOVIES RESULT OUTPUT
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><a href="movielist.php?movie='.$FileName.'"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></a></div>
<div class="rating">
<p><a href="movie-info.php?movie='.$id.'">RATING: </a>'.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}
答案 0 :(得分:1)
一个快速而简单的解决方案就是使用: -
SELECT *,replace(MovieTitle,'The ','') AS sortby FROM Movies ORDER BY sortby;
可能有一个缺点,即标题中其他地方的THE
可能对排序产生影响。 (请注意The
之后的空格,以免影响包含 的字词。
结果如下: -
如果您想确保只忽略第一个 ,那么您可以使用以下内容: -
SELECT *,
CASE
WHEN substr(movietitle,1,4) = 'The '
THEN substr(movietitle,5)
ELSE movietitle
END AS sortby
FROM movies
ORDER by sortby
您也可以通过直接对结果(截断)进行ORDERing来获得额外的列。以前两个查询的等价物可以是: -
SELECT * FROM movies ORDER BY replace(MovieTitle,'The ','');
和
SELECT * from movies ORDER BY CASE WHEN substr(MovieTitle,1,4) = 'The ' THEN substr(MovieTitle,5) ELSE MovieTitle END;