MySQL喜欢多个单词并按重量排序


MySQL where like with multiple words and order by weight

让我们有一个简单的InnoDB表questions包含一列text,其中包含以下数据:

What color does the sun have?
What year it is?
What year was Barack Obama born?
Where in europe people speak french?
When stackoverflow started?

现在,我想搜索此专栏:

SELECT *
FROM `questions`
WHERE `text` LIKE '%What%' OR `text` LIKE '%year%';

但是,这会生成输出:

What color does the sun have?
What year it is?
What year was Barack Obama born?
我希望输出按

搜索单词的出现顺序排序。换句话说,当问题同时包含"什么"和"年份"时,它应该在仅包含"什么"的问题之前。所以输出将如下所示:

What year it is?
What year was Barack Obama born?
What color does the sun have?

这只能使用MySQL来完成吗?如果没有,有没有使用 PHP 做到这一点的好方法?

最简单的方法是将以下order by添加到查询中:

order by (`text` LIKE '%What%') + (`text` LIKE '%year%') desc

如果性能是一个问题,那么您应该研究MySQL全文函数。 它们大大加快了许多全文搜索的速度。