English 中文(简体)
全文搜索标签系统问题
原标题:Full text search - tag system problem

我将标签存储在255 varchar区域中,类似于这种类型;

“,关键字1,关键字2,关键字3,关键字324,”,关键字1234,

(关键字必须以逗号开头和结尾(commakeyword123comma))

-

我可以找到一个类似于这个sql查询的关键字3;

从表中选择*,其中关键字like=%,关键字3,%

CREATE TABLE IF NOT EXISTS `table1` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `tags` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  FULLTEXT KEY `tags` (`tags`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2242 ;


INSERT INTO `table1` (`id`, `tags`) VALUES
(2222,  ,keyword, ),
(2223,  ,word is not big, ),
(2224,  ,关键字3, ),
(2225,  ,my keys, ),
(2226,  ,hello,关键字3,thanks, ),
(2227,  ,hello,thanks,关键字3, ),
(2228,  ,关键字3,hello,thanks, ),
(2239,  ,keyword3 but dont find, ),
(2240,  ,dont find keyword3, ),
(2241,  ,dont keyword3 find, );

(返回2224222622272228)

-

我必须更改全文搜索的类似命令。

从表1中选择*,其中match(tags)against(“,keyword3,”在布尔模式中)

sql命令查找223922402241,但我不想查找%keyword3%或keyword3

http://prntscr.com/137u9

只想找到的想法,关键词3?

,关键字3,

非常感谢。

问题回答

你不能单独使用全文搜索,它只搜索单词。以下是您可以使用的几种不同的替代方案:

  • 您可以使用全文搜索来快速查找候选行,然后在词尾使用LIKE,就像您已经在做的那样,从全文搜索中筛选出任何错误的匹配项。

  • 您可以使用FIND_in_set中。

  • 您可以规范化数据库——每行只存储一个关键字。

    INSERT INTO `table1` (`id`, `tag`) VALUES
    (2222,  keyword ),
    (2223,  word is not big ),
    (2224,  keyword3 ),
    (2225,  my keys ),
    (2226,  hello ),   -- // 2226 has three rows with one keyword in each.
    (2226,  keyword3 ),
    (2226,  thanks ),
    (2227,  hello ),
    -- etc...
    

其中我推荐规范化数据库(如果可能的话)。

首先,全文用于文本搜索。因此,你可以用它做什么是有限制的。要做你想做的事情,你需要查看布尔模式规范,并查看运算符是否可以帮助您,但即使这样,您的搜索也可能不是100%准确。你需要为你的关键词强加一个单词格式(最好不要像<code/>那样在里面使用单词分隔符)。

是否有理由将所有标签存储在一行中?

我会把每个“标签”放在一排,然后按照安德里亚斯的建议做,做这样的事情:

SELECT * FROM table1 WHERE tag IN( keyword0 ,  keyword1 ,  etc. )

如果出于某种原因需要在一行中返回所有标记,可以将它们单独存储,并将GROUP_CONCAT一起存储。

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-凹入





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签