我要在我的内容网站上设计一个搜索网络脚本。
例如这是The mysql DB中的一个文本内容。
A senior U.S. State Department official said Yanukovych had left Kiev for Ukraine's second's largest city of Kharkiv for a meeting after Friday's peace agreement. The official, who spoke on condition of anonymity, had been on the phone with Ukraine's foreign minister.
我搜索“哈尔科夫” 所以我想得到这个结果:
...largest city of Kharkiv for a meeting after...
执行此操作的最佳方法是什么?
我试过这个:
<?php
$_string="Kharkiv";
$db = JFactory::getDBO();
$query = "SELECT CONCAT(REVERSE(SUBSTRING_INDEX(REVERSE(SUBSTR(introtext,1,INSTR(introtext, ".$_string.") - 1)),' ',4)),
SUBSTRING_INDEX(SUBSTR(introtext,INSTR(introtext, ".$_string.")),' ',4))
FROM #__items
WHERE introtext LIKE '%".$_string."%'
";
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $item) {
echo $item->introtext;
}
?>
introtext是item表的一个字段。
最佳答案
您可以使用以下查询:
SELECT CONCAT(
REVERSE(SUBSTRING_INDEX(
REVERSE(SUBSTR(
text,
1,
INSTR(text, 'Kharkiv') - 1
)),
' ',
4
)),
SUBSTRING_INDEX(
SUBSTR(
text,
INSTR(text, 'Kharkiv')
),
' ',
4
)
)
FROM table_name
WHERE text LIKE '%Kharkiv%'
4
是您需要的字数。
关于mysql - 通过 MySQL 在全文中查找单词并返回整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957389/