我爲優雅希望,但這將不得不這樣做。
CASE 1表示90%的用例,開銷可忽略不計。
用例貶值至CASE 5的百分比,適用於2000個案例中的1個。我分析了CASE 4的實際模式,並且模式不一定是詳盡的。
// FIRST, CREATE AN ARRAY OF $LocaleURL PATTERNS, TAKING INTO ACCOUNT THE VARIABLE USE OF HYPHENS
$urlCityArray=explode("-",$LocaleURL);
$countSegs = (count($urlCityArray)); // how many array elements
if ($countSegs == 1) {
$urlCityQuery = $urlCityArray[0];
}
else if ($countSegs == 2) {
$urlCityQuery = $urlCityArray[0] . " " . $urlCityArray[1] . "', '";
$urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1];
// city1 city2, city1-city2
}
else if ($countSegs == 3) {
$urlCityQuery = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . "', '";
$urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . " " . $urlCityArray[2] . "', '";
$urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . "-" . $urlCityArray[2] . "', '";
$urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2];
// city1 city2 city3, city1-city2 city3, city1 city2-city3, city1-city2-city3
}
else if ($countSegs == 4) {
$urlCityQuery = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . " " . $urlCityArray[3] . "', '";
$urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . "-" . $urlCityArray[2] . " " . $urlCityArray[3] . "', '";
$urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2] . "-" . $urlCityArray[3] . "', '";
$urlCityQuery .= $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . "-" . $urlCityArray[3];
// city1 city2 city3 city4, city1 city2-city3 city4, city1-city2-city3-city4, city1 city2 city3-city4
}
else if ($countSegs == 5) {
$urlCityQuery = $urlCityArray[0] . " " . $urlCityArray[1] . " " . $urlCityArray[2] . " " . $urlCityArray[3] . " " . $urlCityArray[4] . "', '";
$urlCityQuery .= $urlCityArray[0] . "-" . $urlCityArray[1] . "-" . $urlCityArray[2] . "-" . $urlCityArray[3] . "-" . $urlCityArray[4];
// Notre-dame-de-lile-perrot
}
SELECT 'city' FROM Cities WHERE 'city' IN (" . $urlCityQuery . ") LIMIT 1;
你真的應該規範化你的輸入數據和數據庫,因爲這樣的查詢很容易會對你的數據庫造成過大的影響。 – Fge 2010-11-11 23:38:51
我們能否看到一些實際出現在數據庫和URL中的數據示例?首先,當你寫(例如)「string string-string」時,不清楚是否有任何重要的順序。另一方面,我們無法看到城市列中的數據格式,並且(如Fge所說)可能有更好的數據結構化方式。 – outis 2010-11-11 23:45:45
正如@Fge所示,規範化您的數據,或使用全文搜索的某種風格。您的搜索應該是精確的(標準化的)或模糊的(「類似Google的搜索」),而不是蠻力。 – deceze 2010-11-11 23:55:57