我正在尝试加入多个表并对它们执行全文搜索。
大多数表是不相关的,但是有相似的字段。
我已经有全文搜索工作,但我需要能够从结果创建链接,这是下一步,但我不瘦k它将工作,因为我没有足够的字段来获得足够的信息。
基本上,我想搜索每个表的标题和内容,但我也想搜索我的论坛表,其中是主题和消息。将链接“主题”和“消息”表。
这个查询不需要查询论坛表就可以了,因为我需要能够搜索那些表。
SELECT * FROM (SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM news WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM events WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM blogs WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM honeylands WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM articles WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM info WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT topicid as title, boardid as content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM articles WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)
UNION ALL
SELECT topicid as title, message as content,
MATCH(title, content) AGAINST('folk*' IN BOOLEAN MODE)
as score FROM info WHERE MATCH(title, content)
AGAINST('folk*' IN BOOLEAN MODE)) a ORDER BY score DESC
我应该能够为具有公共字段名(如events.php)的表创建链接。id=1从记录中获取id,但如何对tables topics和messages topic.php执行此操作?boardID=1&topic=2?
这是我的桌子结构
创建表
articles
(
id
int(4)非空自动递增,
title
varchar(70)不为空默认“”,
content
文本不为空,
主键(
身份证件
)
;
CREATE TABLE `blogs` (
`id` int(3) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `events` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `honeylands` (
`id` int(4) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `info` (
`id` int(1) NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `messages` (
`messageid` int(6) NOT NULL auto_increment,
`boardid` int(2) NOT NULL default '0',
`topicid` int(4) NOT NULL default '0',
`message` text NOT NULL,
`author` varchar(255) NOT NULL default '',
`postdate` datetime default NULL,
PRIMARY KEY (`messageid`)
);
CREATE TABLE `news` (
`id` int(4) NOT NULL auto_increment,
`title` varchar(100) NOT NULL default '',
`content` text NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `topics` (
`topicid` int(4) NOT NULL auto_increment,
`boardid` int(2) NOT NULL default '0',
`topicname` varchar(255) NOT NULL default '',
`author` varchar(255) NOT NULL default '',
`counter` int(5) NOT NULL default '0',
`sticky` char(1) NOT NULL default 'n',
`locked` char(1) NOT NULL default 'n',
PRIMARY KEY (`topicid`)
);
这是我当前获取所有记录的方式,但无法使用union为topics和messages表添加额外字段
SELECT * FROM (SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM news WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM events WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM blogs WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM honeylands WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM articles WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT title, content,
MATCH(title, content) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM info WHERE MATCH(title, content)
AGAINST('$keywords*' IN BOOLEAN MODE)
UNION ALL
SELECT topicname as title,message as content,
MATCH(topicname, message) AGAINST('$keywords*' IN BOOLEAN MODE)
as score FROM topics t INNER JOIN messages m ON t.topicid=m.topicid
WHERE MATCH(topicname, message)
AGAINST('$keywords*' IN BOOLEAN MODE)) a ORDER BY score DESC