简体   繁体   中英

full-text search 2 tables

I have two tables. One stores video information, the other stores the tags associated with that video. They share the common field vid_id . I am trying to fulltext search both tables for matches. The goal is that if there is a match in either table, then all fields with that vid_id be gathered from video.

The problem is my query just crashes with Call to undefined method PDOConnectionFactory::errorInfo() . It should return one row since there is one entry in tags where name field = test . Anyone have any ideas? I have been struggling with this for a while.

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;

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;

$sql = "SELECT video.*
  MATCH(video.title) AGAINST('?') as cscore, 
  MATCH(tags.name) AGAINST('?') as htscore
FROM video
LEFT JOIN tags ON video.vid_id=tags.vid_id
WHERE
  MATCH(video.title) AGAINST('?') OR
  MATCH(tags.name) AGAINST('?')
ORDER BY cscore DESC;";
$stmt4 = $conn->prepare($sql);
$result=$stmt4->execute(array('test','test','test','test')) or die(print_r($db->errorInfo(), true));

The problem is that you're missing a comma (,) after SELECT video.* in your SQL query.

Working example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM