我做的非常相似,什麼東西你描述(在PHP和MySQL)
這是我的代碼:
$search = trim($_GET["search"]);
$searches = explode(" ",$search);
$sql = "SELECT *,wordmatch+descmatch+usagematch+bymatch as `match` FROM (SELECT id,word,LEFT(description,100)as description,
IFNULL((SELECT sum(vote)
FROM vote v
WHERE v.definition_id = d.id),0) as votecount,
";
$sqlword = "";
$sqldesc = "";
$sqlusage = "";
$sqlby = "";
foreach ($searches as $value) {
$value = mysqli_real_escape_string($con,$value);
$sqlword = $sqlword . "+ IFNULL(ROUND((LENGTH(word) - LENGTH(REPLACE(UPPER(word), UPPER('$value'), '')))/LENGTH('$value')),0)";
$sqldesc = $sqldesc . "+ IFNULL(ROUND((LENGTH(description) - LENGTH(REPLACE(UPPER(description), UPPER('$value'), '')))/LENGTH('$value')),0)";
$sqlusage = $sqlusage . "+ IFNULL(ROUND((LENGTH(`usage`) - LENGTH(REPLACE(UPPER(`usage`), UPPER('$value'), '')))/LENGTH('$value')),0)";
$sqlby = $sqlby . "+ IFNULL(ROUND((LENGTH(`by`) - LENGTH(REPLACE(UPPER(`by`), UPPER('$value'), '')))/LENGTH('$value')),0)";
}
$sql = $sql . $sqlword ." as wordmatch,"
. $sqldesc ." as descmatch,"
. $sqlusage ." as usagematch,"
. $sqlby ." as bymatch
FROM definition d
HAVING (wordmatch > 0 OR descmatch > 0 OR usagematch > 0 OR bymatch > 0)
ORDER BY
wordmatch DESC,
descmatch DESC,
usagematch DESC,
bymatch DESC,
votecount DESC)T1";
$queries[] = $sql;
$result = mysqli_query($con,$sql);
你可以在工作中看到http://unurbandictionary.comule.com/view_search.php?search=George+Miley+Cyrus這是當我搜索「George Miley Cyrus」時
它的作用是爆炸搜索字符串以查找每個單詞並返回每個單詞的出現次數,然後我做一個ORDER BY以使相關性(優先級)首先回來。所以在我的情況下word
字段的相關性最高,然後description
字段,然後usage
字段,然後by
字段。
在此版本的代碼之前,我使用的是LIKE
,但它並沒有給我一個出現次數,因爲我希望我的搜索詞最常出現的行先於其他行返回。
不遵循這個問題,'查詢如%查詢和like查詢%'是什麼意思?查詢是否代表字段名稱? – Rottingham
@Rottingham編號它代表搜索項 – Strawberry
我已更改措辭以嘗試並提供幫助。 – Somk