就像query下面一样,显示为score。我想要与匹配的字符串或单词相同

SELECT Opinion, MATCH(Opinion) AGAINST('nice language' IN BOOLEAN MODE) AS Score
FROM opinions
WHERE MATCH(Opinion) AGAINST ('nice language' IN BOOLEAN MODE);


输出:

+---------------------------------------+-------+
| Opinion                               | Score |
+---------------------------------------+-------+
| PHP is a very nice language           |     2 |
| Spain is a nice country to visit      |     1 |
| Perl is not as nice a language as PHP |     2 |
+---------------------------------------+-------+


但是要下面的输出:

+---------------------------------------+--------------+
| Opinion                               | String       |
+---------------------------------------+--------------+
| PHP is a very nice language           | nice language|
| Spain is a nice country to visit      | nice         |
| Perl is not as nice a language as PHP | nice language|
+---------------------------------------+-------+------+

最佳答案

如果您正在寻找如下所示的结果,

php - 如何在全文搜索SQL中获取与列值匹配的字符串-LMLPHP

然后利用以下查询

select Opinion,
      case
        when Opinion like '%nice language%' or (Opinion like '% nice %' and Opinion like '% language %') then 'nice language'
        when Opinion like '% nice %' then 'nice'
        when Opinion like '% language %' then 'language'
      end string
from table1
where
    Opinion like '%nice language%'
      OR (Opinion like '% nice %' and Opinion like '% language %')
      OR Opinion like '% nice %'
      OR Opinion like '% language %'


希望这对您有所帮助。

10-01 09:29