Sql查询匹配和使用正则表达式相关的顺序?

时间:2017-10-25 09:16:15

标签: mysql

您好我正在尝试在sql中使用match和from进行查询以按相关性排序。我不想使用类似的查询或REGEXP,因为我的索引是一个FULLTEXT,它会运行得更快......

这是我的疑问:

id  books              relavance

2   book, folder, paper    '0.15835624933242798'

5  'book                  '0.15835624933242798'

我得到的结果是:

key             type      unique   columns

idx_test_books  FULLTEXT  NO       books

数据表是:

id是INT(11) 书籍LONGTEXT

我拥有的索引:

count()

我想只检索ID为5的书:'book

1 个答案:

答案 0 :(得分:0)

您无法使用FULLTEXT搜索'book,撇号将被忽略。 FULLTEXT索引为builds with words

MariaDB [test]> SELECT *, MATCH(books) AGAINST('"book,.,.,.,$%$:;§$%:;§$%§$:$%"') AS relevance FROM Books ORDER BY relevance;
+----+---------------------+----------------------------+
| id | books               | relevance                  |
+----+---------------------+----------------------------+
|  1 | book, folder, paper | 0.000000001885928302414186 |
|  2 | 'book               | 0.000000001885928302414186 |
+----+---------------------+----------------------------+
2 rows in set (0.00 sec)

MariaDB [test]> SELECT *, MATCH(books) AGAINST('"book,.,.,.,$%$:;§$%folder:;§$%§$:$%"') AS relevance FROM Books ORDER BY relevance;
+----+---------------------+--------------------+
| id | books               | relevance          |
+----+---------------------+--------------------+
|  2 | 'book               |                  0 |
|  1 | book, folder, paper | 0.0906190574169159 |
+----+---------------------+--------------------+
2 rows in set (0.00 sec)

MariaDB [test]> SELECT *, MATCH(books) AGAINST('"book folder"') AS relevance FROM Books ORDER BY relevance;
+----+---------------------+--------------------+
| id | books               | relevance          |
+----+---------------------+--------------------+
|  2 | 'book               |                  0 |
|  1 | book, folder, paper | 0.0906190574169159 |
+----+---------------------+--------------------+
2 rows in set (0.00 sec)