如何为列设置优先级并按该优先级对结果进行排序?
How to set priority for columns and sort the results by that priority?
我有一个 table 这样的:
// mytable
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 1 | hello | how are you? |
| 2 | you | it is content2 |
| 3 | what | hello is a word for greating |
| 4 | mouse | it is content4 |
+----+---------+-------------------------------+
嗯,我想 title
比 content
更优先。我的意思是,我想显示 title
列(第一个)的所有结果,然后显示 content
列(第二个)的所有结果。这也是我的查询:
select * from mytable where match(title, content) against($word);
这里还有两个例子:
示例 1:
$word = 'you';
我想要这样的输出:(关注排序)
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 2 | you | it is content2 |
| 1 | hello | how are you? |
+----+---------+-------------------------------+
示例 2:
$word = 'hello';
我想要这样的输出:(关注排序)
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 1 | hello | how are you? |
| 3 | what | hello is a word for greating |
+----+---------+-------------------------------+
我再次强调,我想要所有来自 title
列的结果,然后所有来自 content
列的结果。有什么解决办法吗?
运行 针对 2 列的 2 个单独查询,并将它们与 union 组合成单个结果集:
select * from mytable where match(title) against($word);
union distinct
select * from mytable where match(content) against($word);
编辑:
如果您不喜欢合并方法,则创建 3 个全文索引,一个在标题上,一个在内容上,一个是两者的组合。然后使用以下方法:
SELECT title, content,
MATCH (title) AGAINST ($word) AS c1,
MATCH (content) AGAINST ($word) AS c2
FROM tablename
WHERE MATCH (title,content) AGAINST ($word)
ORDER BY IF(c1> 0,1,0)
组合索引将用于搜索,而单个索引将用于产生权重。但是,如果一个词被找到的次数太多,它的权重可能会降低为0。
看来你快到了。怎么样:
select * from mytable where match(title) against($word);
union
select * from mytable where match(content) against($word);
您需要做的就是使用 CASE 和单词匹配的条件顺序。这是一个让你开始的例子
SELECT title, content
FROM tablename
ORDER BY CASE
WHEN title LIKE '%you%' THEN 1
WHEN content LIKE '%you%' THEN 2
ELSE 3
END;
我有一个 table 这样的:
// mytable
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 1 | hello | how are you? |
| 2 | you | it is content2 |
| 3 | what | hello is a word for greating |
| 4 | mouse | it is content4 |
+----+---------+-------------------------------+
嗯,我想 title
比 content
更优先。我的意思是,我想显示 title
列(第一个)的所有结果,然后显示 content
列(第二个)的所有结果。这也是我的查询:
select * from mytable where match(title, content) against($word);
这里还有两个例子:
示例 1:
$word = 'you';
我想要这样的输出:(关注排序)
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 2 | you | it is content2 |
| 1 | hello | how are you? |
+----+---------+-------------------------------+
示例 2:
$word = 'hello';
我想要这样的输出:(关注排序)
+----+---------+-------------------------------+
| id | title | content |
+----+---------+-------------------------------+
| 1 | hello | how are you? |
| 3 | what | hello is a word for greating |
+----+---------+-------------------------------+
我再次强调,我想要所有来自 title
列的结果,然后所有来自 content
列的结果。有什么解决办法吗?
运行 针对 2 列的 2 个单独查询,并将它们与 union 组合成单个结果集:
select * from mytable where match(title) against($word);
union distinct
select * from mytable where match(content) against($word);
编辑:
如果您不喜欢合并方法,则创建 3 个全文索引,一个在标题上,一个在内容上,一个是两者的组合。然后使用以下方法:
SELECT title, content,
MATCH (title) AGAINST ($word) AS c1,
MATCH (content) AGAINST ($word) AS c2
FROM tablename
WHERE MATCH (title,content) AGAINST ($word)
ORDER BY IF(c1> 0,1,0)
组合索引将用于搜索,而单个索引将用于产生权重。但是,如果一个词被找到的次数太多,它的权重可能会降低为0。
看来你快到了。怎么样:
select * from mytable where match(title) against($word);
union
select * from mytable where match(content) against($word);
您需要做的就是使用 CASE 和单词匹配的条件顺序。这是一个让你开始的例子
SELECT title, content
FROM tablename
ORDER BY CASE
WHEN title LIKE '%you%' THEN 1
WHEN content LIKE '%you%' THEN 2
ELSE 3
END;