{"id":409,"date":"2022-06-16T10:07:00","date_gmt":"2022-06-16T02:07:00","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=409"},"modified":"2022-06-16T10:07:00","modified_gmt":"2022-06-16T02:07:00","slug":"mysql-%e5%85%a8%e6%96%87%e7%b4%a2%e5%bc%95-%e5%ae%9e%e7%8e%b0%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%90%9c%e7%b4%a2","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2022\/06\/16\/mysql-%e5%85%a8%e6%96%87%e7%b4%a2%e5%bc%95-%e5%ae%9e%e7%8e%b0%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%90%9c%e7%b4%a2\/","title":{"rendered":"MySQL \u5168\u6587\u7d22\u5f15 \u5b9e\u73b0\u76f8\u4f3c\u5ea6\u641c\u7d22"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"%E6%A8%A1%E7%B3%8A%E6%9F%A5%E8%AF%A2\">\u6a21\u7cca\u67e5\u8be2<\/h2>\n\n\n\n<p>Mysql\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2 \u6700\u7b80\u5355\u7684\u662f<code>LIKE<\/code>\u5173\u952e\u5b57, \u5982<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> SELECT * FROM `content` WHERE `topic` LIKE '\n\n\n\n<p>\u800c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>LOCATE()<\/code>,<code>POSITION()<\/code>\u7b49\u5185\u7f6e\u51fd\u6570\u6765\u5b9e\u73b0. \u4e0d\u8fc7 \u8fd9\u79cd\u6a21\u7cca\u67e5\u8be2\u90fd\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027. \u4e3e\u4e2a\ud83c\udf30:<\/p>\n\n\n\n<p>\u8bb0\u5f55\u4e3a: <code>\u4f60\u597d,\u6211\u7684\u4e16\u754c<\/code>, \u6b64\u65f6\u901a\u8fc7\u5173\u952e\u8bcd<code>\u4f60\u597d\u4e16\u754c<\/code> \u4fbf\u65e0\u6cd5\u641c\u7d22\u5230.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3\">\u5982\u4f55\u89e3\u51b3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5728Mysql 5.7.6\u540e Mysql\u5185\u7f6e\u4e86ngram\u5206\u8bcd\u75ab\u60c5, \u53ef\u4ee5\u5b9e\u73b0\u4e2d\u6587, \u65e5\u6587, \u97e9\u6587\u7684\u89e3\u6790. \u6211\u4eec\u9700\u8981\u5bf9\u6307\u5b9a\u5b57\u6bb5\u5efa\u7acb\u5168\u6587\u7d22\u5f15\u5e76\u6307\u5b9a\u5206\u8bcd\u5f15\u64ce.<\/p><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>\u9700\u8981\u6ce8\u610f: \u5efa\u7acb\u5168\u6587\u7d22\u5f15\u7684\u5b57\u6bb5 \u6570\u636e\u7c7b\u578b\u53ea\u80fd\u4e3a VARCHAR, TEXT, CHAR<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E8%AE%BE%E7%BD%AE%E5%88%86%E8%AF%8D\">\u8bbe\u7f6e\u5206\u8bcd<\/h3>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u5148\u8bbe\u7f6engram\u7684\u5206\u8bcd\u957f\u5ea6, \u7531\u4e8e\u4e2d\u6587\u8bcd\u8bed\u4e00\u822c\u4e3a\u4e24\u4e2a\u5b57, \u6240\u4ee5\u5efa\u8bae\u8bbe\u7f6e\u4e3a2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqld --ngram_token_size=2  <\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539mysql\u914d\u7f6e\u6587\u4ef6, \u6307\u5b9a<code>ngram_token_size=2<\/code><\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%BB%BA%E7%AB%8B%E7%B4%A2%E5%BC%95\">\u5efa\u7acb\u7d22\u5f15<\/h3>\n\n\n\n<p>\u9009\u5b9a\u5206\u8bcd\u5f15\u64ce\u5efa\u7acbFLULTEXT\u7d22\u5f15<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE `table_name` ADD FULLTEXT INDEX `index_name`(`column_name`) WITH PARSER ngram;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E8%BF%9B%E8%A1%8C%E6%90%9C%E7%B4%A2\">\u8fdb\u884c\u641c\u7d22<\/h3>\n\n\n\n<p>\u5efa\u7acb\u7d22\u5f15\u540e, \u53ef\u4ee5\u901a\u8fc7 match against\u8bed\u53e5\u8fdb\u884c\u641c\u7d22<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM `table` WHERE MATCH (`column`) against('\u4f60\u597d\u4e16\u754c')<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%85%A8%E6%96%87%E7%B4%A2%E5%BC%95%E7%9A%84%E4%B8%A4%E7%A7%8D%E6%90%9C%E7%B4%A2%E6%A8%A1%E5%BC%8F\">\u5168\u6587\u7d22\u5f15\u7684\u4e24\u79cd\u641c\u7d22\u6a21\u5f0f<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u81ea\u7136\u8bed\u8a00\u641c\u7d22(\u4e5f\u662f\u9ed8\u8ba4\u7684\u641c\u7d22\u6a21\u5f0f): \u4e0d\u80fd\u4f7f\u7528\u64cd\u4f5c\u7b26 \u8fdb\u884c\u590d\u6742\u68c0\u7d22.<\/li><li>BOOLEAN\u6a21\u5f0f : \u53ef\u4ee5\u901a\u8fc7\u64cd\u4f5c\u7b26 \u8fdb\u884c\u590d\u6742\u641c\u7d22, \u4e0e\u641c\u7d22\u5f15\u64ce\u7c7b\u4f3c.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM `table` WHERE MATCH (`column`) against('\u4f60\u597d\u4e16\u754c' IN NATURAL LANGUAGE MODE)\n\n# \u5fc5\u987b\u5305\u542b'\u4f60\u597d', \u4f46\u4e0d\u80fd\u5305\u542b'\u6211\u7684'\nSELECT * FROM `table` WHERE MATCH (`column`) against('+\u4f60\u597d -\u6211\u7684' IN BOOLEAN MODE)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%8F%82%E8%80%83%E6%96%87%E7%AB%A0\">\u53c2\u8003\u6587\u7ae0<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/kbdog.github.io\/2021\/03\/28\/mysql-similiarity-search\" target=\"_blank\" rel=\"noreferrer noopener\">MySql\u76f8\u4f3c\u5ea6\u641c\u7d22<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u7cca\u67e5\u8be2 Mysql\u5b9e\u73b0\u6a21\u7cca\u67e5\u8be2 \u6700\u7b80\u5355\u7684\u662fLIKE\u5173\u952e\u5b57, \u5982 \u800c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f7f\u7528LOCATE(),POSITION()\u7b49\u5185\u7f6e\u51fd\u6570\u6765\u5b9e\u73b0. \u4e0d\u8fc7 \u8fd9\u79cd\u6a21\u7cca\u67e5\u8be2\u90fd\u5b58\u5728\u4e00\u5b9a\u7684\u5c40\u9650\u6027. \u4e3e\u4e2a\ud83c\udf30: \u8bb0\u5f55\u4e3a: \u4f60\u597d,\u6211\u7684\u4e16\u754c, \u6b64\u65f6\u901a\u8fc7\u5173\u952e\u8bcd\u4f60\u597d\u4e16\u754c \u4fbf\u65e0\u6cd5\u641c\u7d22\u5230. \u5982\u4f55\u89e3\u51b3 \u5728Mysql 5.7.6\u540e Mysql\u5185\u7f6e\u4e86ngram\u5206\u8bcd\u75ab\u60c5, \u53ef\u4ee5\u5b9e\u73b0\u4e2d\u6587, \u65e5\u6587, \u97e9\u6587\u7684\u89e3\u6790. \u6211\u4eec\u9700\u8981\u5bf9\u6307\u5b9a\u5b57\u6bb5\u5efa\u7acb\u5168\u6587\u7d22\u5f15\u5e76\u6307\u5b9a\u5206\u8bcd\u5f15\u64ce. \u9700\u8981\u6ce8\u610f: \u5efa\u7acb\u5168\u6587\u7d22\u5f15\u7684\u5b57\u6bb5 \u6570\u636e\u7c7b\u578b\u53ea\u80fd\u4e3a VARCHAR, TEXT, CHAR \u8bbe\u7f6e\u5206\u8bcd \u6211\u4eec\u9700\u8981\u5148\u8bbe\u7f6engram\u7684\u5206\u8bcd\u957f\u5ea6, \u7531\u4e8e\u4e2d\u6587\u8bcd\u8bed\u4e00\u822c\u4e3a\u4e24\u4e2a\u5b57, \u6240\u4ee5\u5efa\u8bae\u8bbe\u7f6e\u4e3a2 \u4e5f\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539mysql\u914d\u7f6e\u6587\u4ef6, \u6307\u5b9angram_token_size=2 \u5efa\u7acb\u7d22\u5f15 \u9009\u5b9a\u5206\u8bcd\u5f15\u64ce\u5efa\u7acbFLULTEXT\u7d22\u5f15 \u8fdb\u884c\u641c\u7d22 \u5efa\u7acb\u7d22\u5f15\u540e, \u53ef\u4ee5\u901a\u8fc7 match against\u8bed\u53e5\u8fdb\u884c\u641c\u7d22 \u5168\u6587\u7d22\u5f15\u7684\u4e24\u79cd\u641c\u7d22\u6a21\u5f0f \u81ea\u7136\u8bed\u8a00\u641c\u7d22(\u4e5f\u662f\u9ed8\u8ba4\u7684\u641c\u7d22\u6a21\u5f0f): \u4e0d\u80fd\u4f7f\u7528\u64cd\u4f5c\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-409","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=409"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/409\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}