全文查询返回重复的行

时间:2011-06-29 07:48:01

标签: mysql sql

当我执行以下全文查询,搜索两个表和两个索引时,我得到重复的数据。 我实际上只知道为什么但是我不知道如何解决它。当我搜索testl时,我得到4个重复,因为在另一个连接表中有4个标记被索引。如果我搜索some new upload,出于同样的原因我会得到2个重复项。有谁知道如何解决这个问题?

CREATE TABLE IF NOT EXISTS `video` (
  `timestamp` int(11) NOT NULL,
  `vid_id` varchar(32) NOT NULL,
  `file_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `uploader` varchar(55) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `title` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `subject_id` int(1) NOT NULL,
  FULLTEXT KEY `title` (`title`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `video`
--

INSERT INTO `video` (`timestamp`, `vid_id`, `file_name`, `uploader`, `title`, `subject_id`) VALUES
(1309290471, 'qcids3qhf95651wp2278f28w2crktaso', '7b682476bfb617a9ca889205c2a11efe', 'geoff', 'untitled', 1),
(1309290896, '6webkscr3pcc7knkg2zm29gkn4fp4eme', '14f5187c74f82b3fe7e4eaf4c3805eee', 'geoff', 'untitled', 1),
(1309291000, 's316v2k9vto73u4b7ap2gv51xr2emmh5', 'e01c5b9c1434330f0e7c2e50a00b2b7a', 'geoff', 'Testl', 7),
(1309299362, 'rdkz0twhvwr2j9mtfhqrrtrh311exhmm', 'e4261879a4f96a0eed3c5f4146bde7e8', 'geoff', 'Some new upload', 6);

CREATE TABLE IF NOT EXISTS `tags` (
  `id` varchar(35) NOT NULL,
  `vid_id` varchar(35) NOT NULL,
  `name` varchar(45) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  UNIQUE KEY `vid_id` (`vid_id`,`name`),
  FULLTEXT KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `tags`
--

INSERT INTO `tags` (`id`, `vid_id`, `name`) VALUES
('c37c449f6677e7980b5f6461efdfacfc', 's316v2k9vto73u4b7ap2gv51xr2emmh5', 'cool'),
('472b9aa1b863e4768eb27fcd19072e5b', 's316v2k9vto73u4b7ap2gv51xr2emmh5', 'o'),
('203c1490e86d734e2674b973a1bd1e26', 's316v2k9vto73u4b7ap2gv51xr2emmh5', 'yea'),
('aff5ec90a82fb22217ce32125e80b54e', 's316v2k9vto73u4b7ap2gv51xr2emmh5', 'p'),
('dd226125d0eaf0c89b06d0d5589ec7a0', 'rdkz0twhvwr2j9mtfhqrrtrh311exhmm', 'hot'),
('40fff048a8bd684eb946ff8400c0a653', 'rdkz0twhvwr2j9mtfhqrrtrh311exhmm', 'hos');

SELECT video.*,
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) as cscore, 
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
WHERE
  MATCH(video.title) AGAINST(? IN BOOLEAN MODE) OR
  MATCH(tags.name) AGAINST(? IN BOOLEAN MODE)
ORDER BY cscore DESC;

1 个答案:

答案 0 :(得分:2)

我认为使用

SELECT DISTINCT video.*,

应该做的伎俩


实际上,没有抱歉没有

MATCH(tags.name) AGAINST(? IN BOOLEAN MODE) as htscore

你可能需要做一些像SUM()htscore和GROUP BY视频。*列