I am trying to join multiple tables and perform a full text search on them.
Most of the tables are unrelated but have similar fields.
I have had the fulltext searches working but i need to be able to create links from the results which is the next step but i don't thin k it will work becuase i haven't got enoygh fields to get enough info.
Basically I want to search for the title and the content of each table but i also want to search my forum tables which are topics and messages. The topics and messages tables are linked.
This query will do the trick without querying the forum tables bu t i need to be able to search those tables.
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
I should be able to create links for the tables that have commom field names such as events.php?id=1 getting the id from the record, but how would i do this for the tables topics and messages topic.php?boardid=1&topic=2 ?
Here is my table structure
CREATE TABLE articles
(
id
int(4) NOT NULL auto_increment,
title
varchar(70) NOT NULL default '',
content
text NOT NULL,
PRIMARY KEY (id
)
);
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`)
);
This is how i currently get all the records but there is no way of adding extra fields for the topics and messages tables using a UNION
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