我有以下搜索电影
select * from movie where title like ? or cast like ? or director like ? or description like ? order by votes desc limit 40
如何通过标题中包含搜索字词的电影订购然后投票?
like order by termInTile, votes desc limit 40
String resultSql = "select * from movie where title like ? or cast like ? or director like ? or description like ? order by votes desc limit 40 ";
statment = connect.prepareStatement(resultSql);
statment.setString(1, "%" + term + "%");
statment.setString(2, "%" + term + "%");
statment.setString(3, "%" + term + "%");
rs = statment.executeQuery();
while (rs.next()) {
if (movies == null) {
movies = new ArrayList<Movie>();
}
movies.add(getMovieFromRs(rs));
}
答案 0 :(得分:2)
您可以使用order by
子句:
select *
from movie
where title like ? or cast like ? or director like ? or description like ?
order by (title like ?) desc,
votes desc
limit 40;
title like ?
将返回一个布尔值,其中1表示真值。 desc
会将这些放在第一位。请注意,在调用查询时,您必须包含其他参数。或者使用命名参数,因此您可以多次引用单个参数。
答案 1 :(得分:0)
回答标题中提出的问题:
SELECT *
FROM movie
WHERE title like ? or cast like ? or director like ? or description like ?
ORDER BY (CASE WHEN title like ? THEN 1 WHEN description like ? THEN 2 ELSE 3 END)
LIMIT 40
;