首先我需要得到完全匹配
SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC
然后使用 LIKE 匹配附加到这些结果查询
SELECT * FROM movies WHERE title LIKE '%STRING&' AND title<>'STRING' ORDER BY x DESC
并将这些结果限制为最多 10 个结果。
UNION 不会做这项工作,因为它将所有结果排序在一起并返回错误的顺序(我需要先完全匹配,然后使用 LIKE)
SELECT * FROM movies WHERE title='STRING' UNION
SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10
我得到的最佳解决方案是使用 multi_query()
$query = "SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC; ";
$query .= "SELECT * FROM movies WHERE title LIKE '%STRING%' AND title<>'red' ORDER BY x DESC";
$Dbi->multi_query($query);
do {
$sql = $Dbi->store_result();
while($x = $sql->fetch_array()) {
...
}
} while($Dbi->next_result());
但在这种情况下,不可能在内循环中使用任何 mysql 并且还必须有更好看的解决方案!