标签和其他相关表

时间:2015-12-08 07:46:12

标签: mysql tags

我想构建一个适合在book.title和tag.tag中搜索的查询 我还想要一个适合计数行的搜索查询分页

这是我的数据库架构和数据

-- Table structure for table `book`
CREATE TABLE IF NOT EXISTS `book` (
`book_id` int(11) NOT NULL,
  `title` varchar(200) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
ALTER TABLE `book`
 ADD PRIMARY KEY (`book_id`);


-- Dumping data for table `book`
INSERT INTO `book` (`book_id`, `title`) VALUES
(1, 'Alice Journey'),
(2, 'Rody Journey'),
(3, 'My Journey to Amsterdam'),
(4, 'How to train your ketty'),
(5, 'The red fox');

-- --------------------------------------------------------


-- Table structure for table `tag`
CREATE TABLE IF NOT EXISTS `tag` (
`tag_id` int(11) NOT NULL,
  `tag` varchar(50) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
ALTER TABLE `tag`
 ADD PRIMARY KEY (`tag_id`);



-- Dumping data for table `tag`
INSERT INTO `tag` (`tag_id`, `tag`) VALUES
(1, 'pets'),
(2, 'tale'),
(3, 'Journey');

-- --------------------------------------------------------

-- Table structure for table `tag_book`
CREATE TABLE IF NOT EXISTS `tag_book` (
  `tag_id` int(11) NOT NULL,
  `book_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `tag_book`
 ADD PRIMARY KEY (`tag_id`,`book_id`);


-- Dumping data for table `tag_book`
INSERT INTO `tag_book` (`tag_id`, `book_id`) VALUES
(1, 4),
(2, 1),
(2, 2),
(2, 5),
(3, 1),
(3, 2),
(3, 3);

search ='rody'//存在于书表中

search ='pets'//存在于标签标签

search ='journey'//存在于书籍和标签表中

谢谢

0 个答案:

没有答案