我正在尝试根据他们的标题相关性对电影进行排序。
以下是原始Myqsl查询结果。
21126 A Frozen Flower
4557 Cashback
4504 Frozen
36645 Frozen
509 Frozen Assets
20649 Frozen Flesh
24752 Frozen Kiss
25675 Frozen Land
4783 Frozen River
11798 Frozen River
11856 Frozen Scream
21755 Frozen Silence
26333 Frozen Thrills
8532 Frozen Water
16196 God Don't Make the Laws
6158 The Frozen Ground
28160 The Thaw
7320 Winter of Frozen Dreams
我正在使用的当前代码
SELECT * FROM `movies`
WHERE `Title` LIKE '%Frozen%'
OR `other_title` LIKE '%Frozen%'
GROUP BY `Title` ORDER BY CASE
WHEN `Title` = 'Frozen' THEN 0
WHEN `Title` like 'Frozen%' THEN 1
WHEN `Title` like '% %Frozen% %' THEN 2
WHEN `Title` like '%Frozen' THEN 3
ELSE 4
END, `Title`
以下是结果
4504 Frozen
509 Frozen Assets
20649 Frozen Flesh
24752 Frozen Kiss
25675 Frozen Land
4783 Frozen River
11856 Frozen Scream
21755 Frozen Silence
26333 Frozen Thrills
8532 Frozen Water
21126 A Frozen Flower
6158 The Frozen Ground
7320 Winter of Frozen Dreams
4557 Cashback
16196 God Don't Make the Laws
28160 The Thaw
好的结果看起来很不错,除了那里有重复的标题,他们没有显示。
任何想法如何解决这个问题??
答案 0 :(得分:2)
group by
正在移除副本。只需删除它:
SELECT *
FROM `movies`
WHERE `Title` LIKE '%Frozen%' OR `other_title` LIKE '%Frozen%'
ORDER BY (CASE WHEN `Title` = 'Frozen' THEN 0
WHEN `Title` like 'Frozen%' THEN 1
WHEN `Title` like '% %Frozen% %' THEN 2
WHEN `Title` like '%Frozen' THEN 3
ELSE 4
END), `Title`
答案 1 :(得分:0)
听起来你可能想在MySQL中使用自然语言搜索功能。这样您就可以将搜索条件与多个字段相匹配,并根据相关性自动对其进行排序。
有关详细信息,请参阅此链接:http://dev.mysql.com/doc/refman/5.5/en/fulltext-natural-language.html